Tuesday, December 30, 2014

JPA @ManyToMany sample

This is a JPA @ManyToMany sample.

@Entity
public class Link {
...

@ManyToMany
@JoinTable(
name = "link_tag",
joinColumns = @JoinColumn(name = "link_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id"))
private List<Tag> tags;

...
}

@Entity
public class Tag {
...

@ManyToMany
@JoinTable(
name = "link_tag",
joinColumns = @JoinColumn(name = "tag_id"),
inverseJoinColumns = @JoinColumn(name = "link_id"))
private List<Link> links;

...
}

Reference:
http://www.java2s.com/Code/Java/JPA/ManyToManyJoinedTable.htm

No comments:

Post a Comment