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 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的更多相关文章
- Java EE (4) -- Java EE 6 Java Persistence API Developer Certified Expert(1z0-898)
Overview of the Java Persistence API Describe the basics of Object Relational Mapping (ORM) Define t ...
- Java Persistence API(转)
定义 Java Persistence API JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.[编辑本段]起源 Sun引入新的JPA ORM规范 ...
- Java Persistence with MyBatis 3(中国版) 第五章 与Spring集成
MyBatis-Spring它是MyBatis子模块框.它用来提供流行的依赖注入框架Spring无缝集成. Spring框架是一个基于依赖注入(Dependency Injection)和面向切面编程 ...
- Java Persistence with MyBatis 3(中国版)
译者的话 前段时间因为工作和学习的须要,我打算深入研究MyBatis框架.于是在网上查找关于MyBatis的教程,发现国内网上关于MyBatis的教程资料少得可怜:除了MyBatis官网上的用户使用手 ...
- JAVA PERSISTENCE API (JPA)
13.2.1. About JPA The Java Persistence API (JPA) is the standard for using persistence in Java proje ...
- Java Persistence with MyBatis 3(中文版) 第五章 与Spring集成
MyBatis-Spring是MyBatis框架的子模块,用来提供与当前流行的依赖注入框架Spring的无缝集成. Spring框架是一个基于依赖注入(Dependency Injection)和面向 ...
- Java Persistence with MyBatis 3(中文版) 第二章 引导MyBatis
MyBatis最关键的组成部分是SqlSessionFactory,我们可以从中获取SqlSession,并执行映射的SQL语句.SqlSessionFactory对象可以通过基于XML的配置信息或者 ...
- Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器
关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务 ...
- Java Persistence with MyBatis 3(中文版) 第一章 MyBatis入门
本章将涵盖以下话题: ž MyBatis是什么? ž 为什么选择MyBatis? ž MyBatis安装配置 ž 域模型样例 1.1 MyBatis是什么 MyBatis是一个简化和实现了Ja ...
随机推荐
- HBase 运维分析
问题分析的主要手段 1.监控系统:首先用于判断系统各项指标是否正常,明确系统目前状况 2.服务端日志:查看例如region移动轨迹,发生了什么动作,服务端接受处理了哪些客户端请求. 3.gc日志:gc ...
- IOS微信分享功能简单实现
PS:此文以简单实现功能为主,不足之前还望指点,大神勿喷. 在此之前如何申请微信认证的Key就不说了,公司一般会有人搞 1.首先下载微信SDK:微信SDK下载地址(更多关于微信SDK信息文档请访问官方 ...
- iOS解决UITableView中Cell重用带来的问题
tableView的常规配置,当超出一屏的cell就会标上可重用的标识出列到可重用缓存池中,后面再根据可重用标识来到的可重的cell就会和前面显示同样内容. - (UITableViewCell *) ...
- 自制node.js + npm绿色版
自制node.js + npm绿色版 Node.js官网有各平台的安装包下载,不想折腾的可以直接下载安装,下面说下windows平台下如何制作绿色版node,以方便迁移. 获取node.exe下载 ...
- js常用 弹出确认 取消对话框
<!DOCTYPE html><html><head> <title></title> <meta charset='utf-8'&g ...
- 高并发教程-基础篇-之nginx负载均衡的搭建
温馨提示:请不要盲目的进行横向扩展,优先考虑对单台服务器的性能优化,只有单台服务器的性能达到最优化之后,集群才会被最大的发挥作用. 一.架构图: 服务器准备:3台,ubuntu16.04系统maste ...
- JAVA物联网九大核心热点技术
1. Unix/Linux平台技术: Unix系统原理.Unix常用命令.Shell编程. 2. Java EE核心技术: Java语言核心.Java高级API.JVM及性能优化.Java Secu ...
- echarts实现中国地图数据展示
在项目中运用到图形展示数据太常见了,echarts是一款使用率非常高的插件工具,很多大平台都是使用echarts: 一般运用到条形.折线.扇形图,今天说一说在中国地图上展示各地数据: 首先要准备中国地 ...
- Visual Studio Live Share不完全指北
Visual Studio Live Share是什么? 是VS的一个实时协作开发的扩展工具. github地址:https://github.com/MicrosoftD...文档地址:https: ...
- flash builder 4.6与myecilpse 10.7集成
一.在flash builder 4.0以后就没有单独提供插件版的flash builder了,因此必须先安装完整版的flash builder,再进行插件集成. 二.集成过程比较简单但也有几个要注意 ...