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 ...
随机推荐
- Class.forName()
主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类, 也就是说JVM会执行该类的静态代码段 ...
- What is Flux?
Pluralsight - React and Flux for Angular Developers 1. An architectural concept. It a idea. 2. Not a ...
- 向openwrt 源码添加ap143支持
借鉴地址:http://www.pppei.net/blog/post/536 1.向文件 \target\linux\ar71xx\generic\profiles\atheros.mk 中添加ap ...
- MC- 挂单STOP交易
using System; using System.Drawing; using System.Linq; using PowerLanguage.Function; using ATCenterP ...
- java反射机制(2)
首先,我们在开始前提出一个问题: 1.在运行时,对于一个java类,能否知道属性和方法:能否去调用它的任意方法? 答案是肯定的. 本节所有目录如下: 什么是JAVA的反射机制 JDK中提供的Refle ...
- DLNA
DLNA最早由索尼.英特尔.微软等提出,全称是DIGITAL LIVING NETWORK ALLIANCE,旨在解决个人PC,消费电器,移动设备在内的无线网络和有线网络的互联互通,其实就是解决电脑和 ...
- python模拟登录知乎
# -*- coding:utf-8 -*- import urllib import urllib2 import cookielib import sys from bs4 import Beau ...
- 在自学php的路上不知道怎么走!!
在自学php的路上不知道怎么走!! 真希望有人给我指点一二!!!
- 定义django admin的站点头,标题等
admin.py from django.contrib import admin from app.models import Product # Register your models here ...
- TCP/IP,http,socket,长连接,短连接
TCP/IP TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议. 在传输层中有TCP协议与UDP协议. 在应 ...