Java Persistence/ManyToMany

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

时间: 2024-08-07 02:26:58

Java Persistence/ManyToMany的相关文章

Java Persistence with MyBatis 3(中文版)

译者的话 前段时间由于工作和学习的需要,我打算深入研究MyBatis框架.于是在网上查找关于MyBatis的教程,发现国内网上关于MyBatis的教程资料少得可怜:除了MyBatis官网上的用户使用手册外,就没有比较系统地讲述MyBatis的教程了. 无意间发现了这本<JavaPersistence with MyBatis 3>,它比较全面地讲述了MyBatis框架.感叹于国内MyBatis资料比较匮乏,故而鼓起勇气自不量力地尝试将此书翻译成中文,希望能够对国内的MyBatis用户有所帮助.

Java Persistence with MyBatis 3(中文版) 第五章 与Spring集成

MyBatis-Spring是MyBatis框架的子模块,用来提供与当前流行的依赖注入框架Spring的无缝集成. Spring框架是一个基于依赖注入(Dependency Injection)和面向切面编程(Aspect Oriented Programming,AOP)的Java框架,鼓励使用基于POJO的编程模型.另外,Spring提供了声明式和编程式的事务管理能力,可以很大程度上简化应用程序的数据访问层(data access layer)的实现.在本章中,我们将看到在基于Spring的

Java Persistence with MyBatis 3(中文版) 前言

对很多软件系统而言,保存数据到数据库和从数据库中检索数据是其工作流程中至关重要的一部分.在 Java 领域,有很多的实现了数据持久化层的工具和框架,它们每一个都有自己不同的实现方法.而 MyBatis,一个简单但功能强大的 Java 持久化框架,则采用了消除冗余代码和充分利用 SQL 和 Java 自身提供的强大的特性的策略. 这本MyBatis 教程将带你经历 MyBatis的安装.配置和使用这几个过程.每一章涉及到的概念将通过简单而实用的例子配合详细的指导来解释. 在本书的最后,你不仅会学到

Java Persistence with MyBatis 3(中文版) 第四章 使用注解配置SQL映射器

在上一章,我们看到了我们是怎样在映射器Mapper XML配置文件中配置映射语句的.MyBatis也支持使用注解来配置映射语句.当我们使用基于注解的映射器接口时,我们不再需要在XML配置文件中配置了.如果你愿意,你也可以同时使用基于XML和基于注解的映射语句. 本章将涵盖以下话题: l 在映射器Mapper接口上使用注解 l 映射语句 @Insert,@Update,@Delete,@SeelctStatements l 结果映射 一对一映射 一对多映射 l 动态SQL @SelectProvi

Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器

关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务器所提供的SQL语句的巨大威力.与此同时,MyBaits消除了书写大量冗余代码的痛苦,它使使用SQL更容易. 在代码里直接嵌套SQL语句是很差的编码实践,并且维护起来困难.MyBaits使用了映射器配置文件或注解来配置SQL语句.在本章中,我们会看到具体怎样使用映射器配置文件来配置映射SQL语句.

Java Persistence with MyBatis 3(中文版) 第一章 MyBatis入门

本章将涵盖以下话题: ž  MyBatis是什么? ž  为什么选择MyBatis? ž  MyBatis安装配置 ž  域模型样例 1.1 MyBatis是什么 MyBatis是一个简化和实现了Java数据持久化层(persistencelayer)的开源框架,它抽象了大量的JDBC冗余代码,并提供了一个简单易用的API和数据库交互. MyBatis的前身是iBATIS,iBATIS于2002年由ClintonBegin创建.MyBatis 3 是iBATIS的全新设计,支持注解和Mapper

Java Persistence with MyBatis 3(中文版) 第二章 引导MyBatis

MyBatis最关键的组成部分是SqlSessionFactory,我们可以从中获取SqlSession,并执行映射的SQL语句.SqlSessionFactory对象可以通过基于XML的配置信息或者Java API 创建. 我们将探索各种MaBatis配置元素,如dataSource,environments,全局参数设置,typeAlias,typeHandlers,SQL映射:接着我们将实例化SqlSessionFactory. 本章将涵盖一下内容: l  使用 XML配置MyBatis

关于注解-Hebernate与JPA(java persistence api)

The JPA spec. defines the JPA annotation in the javax.persistence package. Hibernate not only implements JPA spec , but extends it to adds more features . So , hibernate creates their own annotations which just extend the JPA annotation with the Hibe

Java Persistence with MyBatis 3(中国版) 第五章 与Spring集成

MyBatis-Spring它是MyBatis子模块框.它用来提供流行的依赖注入框架Spring无缝集成. Spring框架是一个基于依赖注入(Dependency Injection)和面向切面编程(Aspect Oriented Programming,AOP)的Java框架,鼓舞使用基于POJO的编程模型. 另外,Spring提供了声明式和编程式的事务管理能力.能够非常大程度上简化应用程序的数据訪问层(data access layer)的实现.在本章中,我们将看到在基于Spring的应用