Java Persistence/ManyToMany

简介: A ManyToMany relationship in Java is where the source object has an attribute that stores a collection of target objects and (if) those target objects...

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

 

目录
相关文章
|
SQL Java API
深入探索Java的持久化技术——JPA(Java Persistence API)
【10月更文挑战第10天】深入探索Java的持久化技术——JPA(Java Persistence API)
864 0
|
Java API 数据库
深入探索Java的持久化技术——JPA(Java Persistence API)
【10月更文挑战第10天】深入探索Java的持久化技术——JPA(Java Persistence API)
648 0
|
存储 缓存 Java
简化持久化操作:深入了解 JPA(Java Persistence API)
在现代的应用程序开发中,数据库持久化操作是一个常见的需求,因此使用一种方便、标准的方式来进行持久化操作非常重要。JPA(Java Persistence API)作为一项 Java 标准,提供了一种标准化的方式来进行对象关系映射(ORM)操作,使得持久化操作更加便捷和规范。在本文中,我们将详细介绍 JPA 的核心概念、特性以及在实际应用中的优势。
1015 0
|
SQL Java 数据库连接
JAVA PERSISTENCE API (JPA)
13.2.1. About JPA The Java Persistence API (JPA) is the standard for using persistence in Java projects.
1572 0
|
Java 数据库连接 API
Java Persistence with Hibernate中文版Hibernate实战第2版勘误
http://www.javaeye.com/topic/179802 Hibernate Spring Java Persistence API EJB3 相关的术语及关键字 还有其它相关的一些链接: Java Persistence with Hibernate中文版Hibernate实战第2版出版 关于Java持久化相关的资源汇集:Java Persistence API 错误难免,有
1979 0
|
10月前
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
443 1
|
10月前
|
JSON 网络协议 安全
【Java基础】(1)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
415 1
|
11月前
|
数据采集 存储 弹性计算
高并发Java爬虫的瓶颈分析与动态线程优化方案
高并发Java爬虫的瓶颈分析与动态线程优化方案
Java 数据库 Spring
463 0
|
11月前
|
算法 Java
Java多线程编程:实现线程间数据共享机制
以上就是Java中几种主要处理多线程序列化资源以及协调各自独立运行但需相互配合以完成任务threads 的技术手段与策略。正确应用上述技术将大大增强你程序稳定性与效率同时也降低bug出现率因此深刻理解每项技术背后理论至关重要.
639 16