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!