JPA 系列教程9-双向一对一唯一外键
双向一对一唯一外键的ddl语句
CREATE TABLE `t_person` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `t_idcard` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`cardNo` varchar(18) DEFAULT NULL,
`person_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_f1wp2l5dnl1w6sprob8ocil2t` (`person_id`),
CONSTRAINT `FK_f1wp2l5dnl1w6sprob8ocil2t` FOREIGN KEY (`person_id`) REFERENCES `t_person` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Person
package com.jege.jpa.one2one;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:关系维护端
*/
@Entity
@Table(name = "t_person")
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
// mappedBy配置映射关系:当前对象IdCard属于哪个person对象
@OneToOne(optional = false, mappedBy = "person", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private IdCard idCard;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
IdCard
package com.jege.jpa.one2one;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:关系被维护端,多表(唯一unique = true)
*/
@Entity
@Table(name = "t_idcard")
public class IdCard {
@Id
@GeneratedValue
private Long id;
@Column(length = 18)
private String cardNo;
// 默认值optional = true表示idcard_id可以为空;反之。。。
@OneToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "person_id", unique = true)
// unique=true确保了一对一关系
private Person person;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
One2OneTest
package com.jege.jpa.one2one;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:一对一CRUD Test
*/
public class One2OneTest {
private static EntityManagerFactory entityManagerFactory = null;
private EntityManager entityManager = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");
}
@Before
public void setUp() throws Exception {
entityManager = entityManagerFactory.createEntityManager();
}
// 级联保存
@Test
public void persist() throws Exception {
Person person = new Person();
person.setName("jege");
IdCard idCard = new IdCard();
idCard.setCardNo("123456789123456789");
person.setIdCard(idCard);
idCard.setPerson(person);
entityManager.getTransaction().begin();
entityManager.persist(person);
entityManager.getTransaction().commit();
}
@Test
public void find() throws Exception {
persist();
entityManager.clear();
Person person = entityManager.find(Person.class, 1L);
System.out.println(person.getName());
System.out.println("-----------------");
System.out.println(person.getIdCard().getCardNo());
}
@Test
public void find1() throws Exception {
persist();
entityManager.clear();
IdCard idCard = entityManager.find(IdCard.class, 1L);
System.out.println(idCard.getCardNo());
System.out.println("-----------------");
System.out.println(idCard.getPerson().getName());
}
@After
public void tearDown() throws Exception {
if (entityManager != null && entityManager.isOpen())
entityManager.close();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
if (entityManagerFactory != null && entityManagerFactory.isOpen())
entityManagerFactory.close();
}
}
其他关联项目
- JPA 系列教程8-双向一对一共享主键
http://blog.csdn.net/je_ge/article/details/53495313
源码地址
如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!
JPA 系列教程9-双向一对一唯一外键的更多相关文章
- JPA 系列教程8-双向一对一共享主键
双向一对一共享主键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...
- JPA 系列教程10-双向一对一关联表
双向一对一关联表的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255 ...
- 012一对一 唯一外键关联映射_双向(one-to-one)
² 两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ² 有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...
- 011一对一 唯一外键关联映射_单向(one-to-one)
² 两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ² 有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库 ...
- java:Hibernate框架2(关联映射(多对一,一对多,一对多双向,一对一主键,一对一唯一外键,多对多双向))
hibernate.cfg.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE h ...
- jpa双向一对一关联外键映射
项目结构: Wife package auth.model; import javax.persistence.CascadeType; import javax.persistence.Column ...
- JPA 系列教程7-双向多对多
双向多对多的ddl语句 同单向多对多表的ddl语句一致 Student package com.jege.jpa.many2many; import java.util.HashSet; import ...
- JPA 系列教程5-双向一对多
双向一对多的ddl语句 同单向多对一,单向一对多表的ddl语句一致 Product package com.jege.jpa.one2many; import javax.persistence.En ...
- JPA 系列教程4-单向一对多
JPA中的@OneToMany @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface OneToMany { Class tar ...
随机推荐
- nginx读取图片没有权限
场景是这样的,我们项目中上传图片到linux服务器后,保存图片存储路径和网络访问路径.app中用数据库返回的 图片访问路径来访问图片(nginx通过nfs方式读取图片).但是访问不到.要手动 chmo ...
- Gym 100553B Burrito King 无脑背包
题意就是你有n和m两个上限 吃一个东西会同时增加两个东西 m的值不能超过给定的m 问最后的n m值和每个东西吃了多少 贪心一下就好了 算一下性价比 从最大的开始吃 直到吃满了m n也一定是最大了 只是 ...
- DOS 命令批量删除文件及相关批处理命令详解
del X:\*.* /f /s /q /a 递归强制静默删除X盘及其所有子目录下的所有文件 /f 表示强制删除文件 /s表示子目录都要删除该文件 /q表示无声,不提示 /a根据属性选择要删除的文件 ...
- 2015 Multi-University Training Contest 3
1001 Magician 线段树.根据奇偶性分成4个区间.维护子列和最大值. 想法很简单.但是并不好写. 首先初始化的时候对于不存在的点要写成-INF. 然后pushup的时候.对于每个区间要考虑四 ...
- CDOJ 1324 卿学姐与公主 分块
题目地址 分块模板 #include<cstdio> #include<algorithm> #include<math.h> using namespace st ...
- Nginx “邪恶” rewrite
概述 本文主要针对nginx rewrite指令困惑的地方进行讲解,中间会涉及部分原理部分,我尽量用通俗易懂的语言来形容 功能讲解 大致流程 The ngx_http_rewrite_module m ...
- 解决Eclipse无法添加Tomcat服务器的问题
eclipse配置好以后,如果Tomcat服务器在文件系统的位置发生了变化,则需要重新配置Tomcat服务器,这时会遇到无法设置服务器的问题 即图中框起来的部分无法进行操作,这时需要 关闭Eclips ...
- Codeforces Round #375 (Div. 2)A. The New Year: Mee
A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...
- 《Windows驱动开发技术详解》之分层驱动程序
分层驱动程序概念 分层的目的是将功能复杂的驱动程序分解成多个简单的驱动程序.一般来说,他们是指两个或两个 以上的驱动程序,它们分别创建设备对象,并且形成一个由高到低的设备对象栈.IRP请求一般会被传送 ...
- Windows进程间通信(中)
二.文件映射 文件映射(Memory-Mapped Files)能使进程把文件内容当作进程地址区间一块内存那样来对待.因此,进程不必使用文件I/O操作,只需简单的指针操作就可读取和修改文件的内容. W ...