博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Persistence/ManyToMany
阅读量:6250 次
发布时间:2019-06-22

本文共 2296 字,大约阅读时间需要 7 分钟。

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  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  annotation or the <many-to-many> element.

All ManyToMany relationships require a JoinTable. The JoinTable is defined using the  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[]

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[]

@Entitypublic 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
projects; ..... }

Example of a ManyToMany relationship XML[]

https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

 

转载地址:http://gafsa.baihongyu.com/

你可能感兴趣的文章
Linux ld命令
查看>>
在 Word 中的受支持的区域设置标识符的列表
查看>>
No package的问题解决
查看>>
【转】chrome浏览器的跨域设置——包括版本49前后两种设置
查看>>
母牛的故事
查看>>
Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置说明2
查看>>
javaScript基础练习题-下拉框制作
查看>>
基于 OAuth 安全协议的 Java 应用编程1
查看>>
使用Golang利用ectd实现一个分布式锁
查看>>
javaweb学习总结五(内省、beanUtils工具包)
查看>>
An easy to use android color picker library
查看>>
iOS10全新推送功能的实现
查看>>
C#中容易被忽视的细节整理
查看>>
php内核分析(二)-ZTS和zend_try
查看>>
获取form对象
查看>>
不确定人数的抽奖方法
查看>>
win7 windows server 2008R2下 https SSL证书安装的搭配(搭配https ssl本地测试环境)
查看>>
sh脚本——#!/bin/bash
查看>>
MYSQL-innodb性能优化几个点
查看>>
什么是Mixin模式:带实现的协议
查看>>