I need to create a join table in my database using JPA
annotations so the result will be this:
So far I just implemented 2 entities:
@Entity
@Table(name="USERS", schema="ADMIN")
public class User implements Serializable {
private static final long serialVersionUID = -1244856316278032177L;
@Id
@Column(nullable = false)
private String userid;
@Column(nullable = false)
private String password;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@Entity
@Table(name="GROUPS", schema="ADMIN")
public class Group implements Serializable {
private static final long serialVersionUID = -7274308564659753174L;
@Id
@Column(nullable = false)
private String groupid;
public String getGroupid() {
return groupid;
}
public void setGroupid(String groupid) {
this.groupid = groupid;
}
}
Should i create another entity called USER_GROUP
or i can just add some annotations, so the join table will be created automatically when i run create tables from entities(ORM)?
How should i annotate my entities to achieve the same as in the image?
A join table is typically used in the mapping of many-to-many and unidirectional one-to-many associations. It may also be used to map bidirectional many-to-one/one-to-many associations, unidirectional many-to-one relationships, and one-to-one associations (both bidirectional and unidirectional).
Joining Tables with JPA Specifications select author0_.id as id1_1_, author0_. first_name as first_na2_1_, author0_. last_name as last_nam3_1_ from author author0_ inner join author_books books1_ on author0_.id = books1_. author_id inner join book book2_ on books1_.
We can do this with the @JoinTable annotation in the Student class. We provide the name of the join table (course_like) as well as the foreign keys with the @JoinColumn annotations.
You definitely shouldn't create User_Group entity as it's more the underlying database representation than the object oriented one.
You can achieve the join table by defining something like:
@Entity @Table(name="USERS", schema="ADMIN") public class User implements Serializable { //... @ManyToOne @JoinTable(name="USER_GROUP") Group group;
@Entity @Table(name="GROUPS", schema="ADMIN") public class Group implements Serializable { //... @OneToMany(mappedBy="group") Set<User> users;
Edit: If you want to explicitly set the names of the columns you could use @JoinColumn elements as shown below:
@ManyToOne @JoinTable(name="USER_GROUP", joinColumns = @JoinColumn(name = "userid", referencedColumnName = "userid"), inverseJoinColumns = @JoinColumn(name = "groupid", referencedColumnName = "groupid")) Group group;
I would implement it this way:
@Entity @Table(name="GROUPS", schema="ADMIN") public class Group implements Serializable { @OneToMany @JoinTable(name = "USER_GROUP", joinColumns = @JoinColumn(name = "groupid"), inverseJoinColumns = @JoinColumn(name = "userid")) private List<User> users; }
Solution suggested by @PedroKowalski should work too, but then you'll have to keep a reference to Group entity in your User entity which is not always possible.
To have the same annotations like in your diagram you can do this in your User
class:
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "USER_GROUP",
joinColumns = { @JoinColumn(name = "userid") },
inverseJoinColumns = { @JoinColumn(name = "groupid") })
private List<Group> grups;
in your group class
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "USER_GROUP",
joinColumns = { @JoinColumn(name = "groupid") },
inverseJoinColumns = { @JoinColumn(name = "userid") })
private List<User> users;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With