双向一对一共享主键

双向一对一共享主键

双向一对一共享主键的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 [email protected]
* @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 [email protected]
* @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 [email protected]
* @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-双向一对一共享主键-LMLPHP

JPA 系列教程8-双向一对一共享主键-LMLPHP

05-10 19:05