Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create join table with JPA annotations?

I need to create a join table in my database using JPA annotations so the result will be this:

enter image description here

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?

like image 715
javing Avatar asked Nov 02 '11 11:11

javing


People also ask

What is a join table JPA?

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).

How can I join JPA specification?

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_.

Which annotation is used to link two tables through a relation table JPA?

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.


3 Answers

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; 
like image 172
Piotr Nowicki Avatar answered Oct 08 '22 19:10

Piotr Nowicki


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.

like image 32
jFrenetic Avatar answered Oct 08 '22 21:10

jFrenetic


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;
like image 23
user902383 Avatar answered Oct 08 '22 21:10

user902383