Reputation: 880
I am currently working with tables with multiple one to many relationships and I'm trying to implement all of them using Hibernate.
For example, three tables I have are:
Product, Users, Group
Group is in an one-to-many relationship with Users
Product is also in an one-to-many relationship with Users
Since Users is in a many-to-one relationship with both Product and Group, would my current implementation of Users.java be the correct way of implementation by including two ManyToOne annotations?
Also, is it better to write the ManyToOne annotations right above the get methods (in this case, above getProduct() and above getGroup()) or to write them right above the class variables?
@Entity
@Table(name = "users")
public class Users {
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name = "product_id")
private Product product;
@ManyToOne
@JoinColumn(name = "group_id")
private Group group;
@Column(name = "user_name")
private String userName;
public Users(){}
public Users(Product product, Group group, String userName) {
this.product = product;
this.group = group;
this.userName = userName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public product getProduct() {
return product;
}
public Group getGroup(){
return group;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Thank you for your help!
Upvotes: 0
Views: 817
Reputation: 5721
Since Users is in a many-to-one relationship with both Product and Group, would my current implementation of Users.java be the correct way of implementation by including two ManyToOne annotations?
Yes, it is the correct way(If you want just the basic ManyToOne mapping) and you can have more than one ManyToOne Mapping in a class .
If you need any Cascading
or FetchType
or a different class (targetEntity
) to be defined then you'll need the add those.
Also, is it better to write the ManyToOne annotations right above the get methods (in this case, above getProduct() and above getGroup()) or to write them right above the class variables?
It mainly depends on whether you want to do some processing before saving an Object.
Like sending a default value to DB in case the value of the object is null, etc.
I personally prefer using hibernate annotations on class variables. Here is a link which explains this clearly.
Either approaches you take, hibernate will look for where @Id
annotation is defined (At method or variable level) to decide which scan it should choose for other fields.
Pasting some details provided in Spring documentation which somewhat address the same question but for spring.
Method and Field level Injection
Annotations that indicate dependency injections (such as @PersistenceUnit and @PersistenceContext) can be applied on field or methods inside a class, therefore the expression "method/field level injection". Field-level annotations concise and easier to use while method-level allow for processing the injected dependency. In both cases the member visibility (public, protected, private) does not matter.
Upvotes: 1