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 ManyToMany
relationship. 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 ManyToMany
relationship 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 ...
随机推荐
- LeetCode之“动态规划”:Word Break && Word Break II
1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...
- objc写一个NSMutableArray不连续索引替换对象的方法
NSMutableArray内置的方法-(void)replaceObjectsAtIndexes:(NSIndexSet*)set withObjects:(NSArray*)objs 只能替换一段 ...
- 【Android 应用开发】Android游戏音效实现
1. 游戏音效SoundPool 游戏中会根据不同的动作 , 产生各种音效 , 这些音效的特点是短暂(叫声,爆炸声可能持续不到一秒) , 重复(一个文件不断重复播放) , 并且同时播放(比如打怪时怪的 ...
- OpenShift实战(七):OpenShift定制镜像S2I
1.基础镜像制作 由于公司的程序是Java开发,上线发布使用的是maven,如果使用openshift自带的S2I,每次都会全量拉取代码(代码比较多,每次全量拉太慢),然后每次打包都会再一次下载mav ...
- OC实现带弹跳动画按钮的界面控制器view
很多应用都有带弹跳动画发布界面,这里用一个 UIViewController 实现这种效果,外界只要 modal出不带动画这个控制器就可以实现 #import "BSPublishVC.h& ...
- JQuery系统梳理
JQuery在前端网页开发中可以说是非常常用了,它所拥有的强大功能足以让我们完成各式各样的效果. 一.JQuery基础语法 1. 使用JQuery必须先导入jquery.x.x.x.js文件: 2. ...
- 详解k8s组件Ingress边缘路由器并落地到微服务 - kubernetes
写在前面 Ingress 英文翻译 进入;进入权;进食,更准确的讲就是入口,即外部流量进入k8s集群必经之口.这到大门到底有什么作用?我们如何使用Ingress?k8s又是如何进行服务发现的呢?先看一 ...
- 手把手教你全家桶之React(二)
前言 上一篇已经讲了一些react的基本配置,本遍接着讲热更新以及react+redux的配置与使用. 热更新 我们在实际开发时,都有用到热更新,在修改代码后,不用每次都重启服务,而是自动更新.并而不 ...
- JavaScript之对象继承
原型链继承 function SuperType(){ this.property = true; } SuperType.prototype.getSuperValue = function(){ ...
- require、缓存
什么是require? -Node使用CommonJS模块规范,内置require函数用于加载模块文件 -require的基本功能是>读入并执行一个javascript文件,然后返回该模块的ex ...