码迷,mamicode.com
首页 > 编程语言 > 详细

Java Persistence/ManyToMany

时间:2017-01-17 21:32:35      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:org   tor   index   wiki   where   prim   query   tab   java   

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)

ID FIRSTNAME LASTNAME
1 Bob Way
2 Sarah Smith

EMP_PROJ (table)

EMP_ID PROJ_ID
1 1
1 2
2 1

PROJECT (table)

ID NAME
1 GIS
2 SIG

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

 

Java Persistence/ManyToMany

标签:org   tor   index   wiki   where   prim   query   tab   java   

原文地址:http://www.cnblogs.com/softidea/p/6294445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!