双向一对一共享主键的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,
`cardNo` varchar(18) DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_kq6it0i1ktl2w8mc9hifq4ov3` FOREIGN KEY (`id`) REFERENCES `t_person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Person

package com.jege.jpa.one2one;

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", 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.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:关系被维护端,共享主键
*/
@Entity
@Table(name = "t_idcard")
public class IdCard {
@Id
@GeneratedValue(generator = "pkGenerator")
@GenericGenerator(name = "pkGenerator", strategy = "foreign", parameters = @Parameter(name = "property", value = "person"))
private Long id;
@Column(length = 18)
private String cardNo;
@OneToOne(optional = false, fetch = FetchType.LAZY)
// 如果不加这个注解,添加t_idcard信息时,就会自动在t_idcard表中增加了一个外键person_id
@PrimaryKeyJoinColumn
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.persist(idCard);
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();
} }

源码地址

https://github.com/je-ge/jpa

如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!



JPA 系列教程8-双向一对一共享主键的更多相关文章

  1. JPA 系列教程9-双向一对一唯一外键

    双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...

  2. JPA 系列教程10-双向一对一关联表

    双向一对一关联表的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255 ...

  3. JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable

    复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id和复合主键-2个@Id+ ...

  4. JPA 系列教程12-复合主键-2个@Id+@IdClass

    复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id一样 Airline p ...

  5. JPA 系列教程7-双向多对多

    双向多对多的ddl语句 同单向多对多表的ddl语句一致 Student package com.jege.jpa.many2many; import java.util.HashSet; import ...

  6. JPA 系列教程4-单向一对多

    JPA中的@OneToMany @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface OneToMany { Class tar ...

  7. JPA 系列教程2-单表操作

    JPA Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.他的出现主要是为了简化现有的持久化开发工作和整合ORM技术,结束现在Hibe ...

  8. JPA 系列教程21-JPA2.0-@MapKeyColumn

    @MapKeyColumn 用@JoinColumn注解和@MapKeyColumn处理一对多关系 ddl语句 CREATE TABLE `t_employee` ( `id` bigint(20) ...

  9. JPA 系列教程17-继承-独立表-TABLE_PER_CLASS

    PerTable策略 每个具体的类一个表的策略 举例 这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列. 这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到 ...

随机推荐

  1. KindeEditor图片上传插件用法

    因业务需要找了款插件 KindeEditor编辑器确认挺好用,但无奈技术有限,上传配置不知,故问度娘! 图片上传对于部分新手来说有时候是一件非常头疼的事,今天来分享一下项目中使用到的这个插件Kinde ...

  2. ggplot2 scale相关设置-坐标转换

    ggplot2 scale相关设置-坐标转换 在R中坐标轴转换有多种形式,包括对数转换,平方根转换以及坐标刻度前后进行调换 用到的函数分别有: scale_x_log10(...) scale_y_l ...

  3. ggplot2 scale相关设置2—时间设置

    在scale设置中,常用的日期方面的设置函数包括: scale_x_date(),scale_y_date(),scale_x_datetime(),scale_y_datetime()   接下来, ...

  4. poj 1142 Smith Numbers

    Description While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh U ...

  5. angularjs表单

    <form ng-app="myApp" ng-controller="validateCtrl" name="myForm" nov ...

  6. 一些css3的特效 javascript的window对象 定时器 延时器等ing...

    风车转动代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  7. NFS挂载故障卡死的问题

    NFS挂载故障卡死的问题 默认是硬的,改成软的.比如:mount -t nfs -o rw,vers=4,noacl,nocto,noatime,nodiratime,rsize=131072,wsi ...

  8. 2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt

    package zuoye; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...

  9. 自定义Button成进度条

    ProgressButton源码: package com.example.progressbutton; import android.content.Context; import android ...

  10. Bourn Again Shell编程

    shell既是命令解释程序,又是一种高级程序设计语言.shell是解释型语言. bash脚本的建立和运行: 注释行以#开头 #!后面的参数告诉系统执行本文件的程序 执行脚本文件有两种方法: 1.   ...