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

 

相关文章
|
8月前
|
存储 缓存 Java
简化持久化操作:深入了解 JPA(Java Persistence API)
在现代的应用程序开发中,数据库持久化操作是一个常见的需求,因此使用一种方便、标准的方式来进行持久化操作非常重要。JPA(Java Persistence API)作为一项 Java 标准,提供了一种标准化的方式来进行对象关系映射(ORM)操作,使得持久化操作更加便捷和规范。在本文中,我们将详细介绍 JPA 的核心概念、特性以及在实际应用中的优势。
211 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.
1356 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 错误难免,有
1556 0
|
7天前
|
存储 Java 数据库连接
java多线程之线程通信
java多线程之线程通信
|
7天前
|
安全 Java 开发者
深入理解Java并发编程:线程安全与性能优化
【4月更文挑战第9天】本文将深入探讨Java并发编程的核心概念,包括线程安全和性能优化。我们将详细解析Java中的同步机制,包括synchronized关键字、Lock接口以及并发集合等,并探讨它们如何影响程序的性能。此外,我们还将讨论Java内存模型,以及它如何影响并发程序的行为。最后,我们将提供一些实用的并发编程技巧和最佳实践,帮助开发者编写出既线程安全又高效的Java程序。
20 3
|
10天前
|
设计模式 安全 Java
Java并发编程实战:使用synchronized关键字实现线程安全
【4月更文挑战第6天】Java中的`synchronized`关键字用于处理多线程并发,确保共享资源的线程安全。它可以修饰方法或代码块,实现互斥访问。当用于方法时,锁定对象实例或类对象;用于代码块时,锁定指定对象。过度使用可能导致性能问题,应注意避免锁持有时间过长、死锁,并考虑使用`java.util.concurrent`包中的高级工具。正确理解和使用`synchronized`是编写线程安全程序的关键。
|
8天前
|
Java
Java 并发编程:深入理解线程池
【4月更文挑战第8天】本文将深入探讨 Java 中的线程池技术,包括其工作原理、优势以及如何使用。线程池是 Java 并发编程的重要工具,它可以有效地管理和控制线程的执行,提高系统性能。通过本文的学习,读者将对线程池有更深入的理解,并能在实际开发中灵活运用。
|
7天前
|
算法 Java 开发者
Java中的多线程编程:概念、实现与性能优化
【4月更文挑战第9天】在Java编程中,多线程是一种强大的工具,它允许开发者创建并发执行的程序,提高系统的响应性和吞吐量。本文将深入探讨Java多线程的核心概念,包括线程的生命周期、线程同步机制以及线程池的使用。接着,我们将展示如何通过继承Thread类和实现Runnable接口来创建线程,并讨论各自的优缺点。此外,文章还将介绍高级主题,如死锁的预防、避免和检测,以及如何使用并发集合和原子变量来提高多线程程序的性能和安全性。最后,我们将提供一些实用的性能优化技巧,帮助开发者编写出更高效、更稳定的多线程应用程序。
|
5天前
|
安全 算法 Java
深入理解Java并发编程:线程安全与性能优化
【4月更文挑战第11天】 在Java中,高效的并发编程是提升应用性能和响应能力的关键。本文将探讨Java并发的核心概念,包括线程安全、锁机制、线程池以及并发集合等,同时提供实用的编程技巧和最佳实践,帮助开发者在保证线程安全的前提下,优化程序性能。我们将通过分析常见的并发问题,如竞态条件、死锁,以及如何利用现代Java并发工具来避免这些问题,从而构建更加健壮和高效的多线程应用程序。
|
9天前
|
Java
Java并发编程:深入理解线程池
【4月更文挑战第7天】在现代软件开发中,多线程编程已经成为一种不可或缺的技术。为了提高程序性能和资源利用率,Java提供了线程池这一强大工具。本文将深入探讨Java线程池的原理、使用方法以及如何根据实际需求定制线程池,帮助读者更好地理解和应用线程池技术。
15 0