Java Persistence/ManyToMany

本文详细介绍了Java中ManyToMany关系的概念及其实现方式。包括使用@ManyToMany注解定义ManyToMany关系,以及如何通过JoinTable指定关联表的结构。同时对比了ManyToMany与OneToMany关系的区别。

ManyToMany relationship in Java is where the source object has an attribute that stores a collection of target objects and (if) those target objects had the inverse relationship back to the source object it would also be a ManyToManyrelationship. All relationships in Java and JPA are unidirectional, in that if a source object references a target object there is no guarantee that the target object also has a relationship to the source object. This is different than a relational database, in which relationships are defined through foreign keys and querying such that the inverse query always exists.

JPA also defines a OneToMany relationship, which is similar to a ManyToMany relationship except that the inverse relationship (if it were defined) is a ManyToOne relationship. The main difference between a OneToMany and a ManyToManyrelationship in JPA is that a ManyToMany always makes use of a intermediate relational join table to store the relationship, where as a OneToMany can either use a join table, or a foreign key in target object's table referencing the source object table's primary key.

In JPA a ManyToMany relationship is defined through the @ManyToMany annotation or the <many-to-many> element.

All ManyToMany relationships require a JoinTable. The JoinTable is defined using the @JoinTable annotation and <join-table> XML element. The JoinTable defines a foreign key to the source object's primary key (joinColumns), and a foreign key to the target object's primary key (inverseJoinColumns). Normally the primary key of the JoinTable is the combination of both foreign keys.

Example of a ManyToMany relationship database[edit]

EMPLOYEE (table)

IDFIRSTNAMELASTNAME
1BobWay
2SarahSmith

EMP_PROJ (table)

EMP_IDPROJ_ID
11
12
21

PROJECT (table)

IDNAME
1GIS
2SIG

Example of a ManyToMany relationship annotation[edit]

@Entity
public class Employee {
  @Id @Column(name="ID") private long id; ... @ManyToMany @JoinTable( name="EMP_PROJ", joinColumns=@JoinColumn(name="EMP_ID", referencedColumnName="ID"), inverseJoinColumns=@JoinColumn(name="PROJ_ID", referencedColumnName="ID")) private List<Project> projects; ..... } 

Example of a ManyToMany relationship XML[edit]

<entity name="Employee" class="org.acme.Employee" access="FIELD"> <attributes> <id name="id"> <column name="EMP_ID"/> </id> <set name="projects" table="EMP_PROJ" lazy="true" cascade="none" sort="natural" optimistic-lock="false"> <key column="EMP_ID" not-null="true" /> <many-to-many class="com.flipswap.domain.Project" column="PROJ_ID" /> </set> </attributes> </entity>

https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值