-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Many to one Annotations
PostPosted: Thu Feb 10, 2011 3:46 am 
Newbie

Joined: Thu Feb 10, 2011 3:36 am
Posts: 1
Hi,

I have two tables (customers and clients) and for every client there are many customers, my problem is, if i retrieve the customers record, i want to get the client record as well, i want to know how will i do that using many to one annotation or any solution.

this is my customer class

@Entity
@Table(name = "customers", catalog = "sample")
public class Customers implements java.io.Serializable {

private Integer id;
private Integer clientId;
private String firstName;
private String lastName;

public Customers(Integer id, Integer clientId, String firstName, String lastName) {
this.id = id;
this.clientId = clientId;
this.firstName = firstName;
this.lastName = lastName;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

@Column(name = "first_name", length = 15)
public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

@Column(name = "last_name", length = 15)
public String getLastName() {
return this.lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

//I dont exactly know if this is correct
public Clients getClients() {
return clients;
}

public void setClients(Clients clients) {
this.clients = clients;
}
}

and this is my client class


@Entity
@Table(name = "clients", catalog = "lightning_tote_db")
public class Clients implements java.io.Serializable {

private Integer id;
private String name;
private String email;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

@Column(name = "name", length = 25)
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@Column(name = "email", length = 25)
public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}

}


thanks!


Top
 Profile  
 
 Post subject: Re: Many to one Annotations
PostPosted: Thu Feb 10, 2011 5:41 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

You datamodel is very confusing and not very clear ...
You should start by renaming your classes Customer and Client because they both contain the data of only one Customer/Client

You want every Client to have many Customers so you should implement a List of Customer in your Client class and annotate it with @OneToMany ...
Now you also want a Customer to know about his Client, so in fact you want your relation to be bidirectionnal ... you need to add a property client of type Client in your class Customer and annotate it with @ManyToOne ...
You can search in hibernate's documentation for more information on the properties of those 2 annotations


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.