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

双向一对一关联表的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,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

CREATE TABLE `t_person_idcard` (
  `idcard_id` bigint(20) DEFAULT NULL,
  `person_id` bigint(20) NOT NULL,
  PRIMARY KEY (`person_id`),
  UNIQUE KEY `UK_jx3n0kvh4nsob0353uxdy2h4i` (`idcard_id`),
  CONSTRAINT `FK_ad8ywwmyp031ems3uc04ht3av` FOREIGN KEY (`person_id`) REFERENCES `t_person` (`id`),
  CONSTRAINT `FK_jx3n0kvh4nsob0353uxdy2h4i` FOREIGN KEY (`idcard_id`) REFERENCES `t_idcard` (`id`)
) ENGINE=InnoDB 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.JoinColumn;
import javax.persistence.JoinTable;
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;
  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  @JoinTable(name = "t_person_idcard", joinColumns = @JoinColumn(name = "person_id"), inverseJoinColumns = @JoinColumn(name = "idcard_id", unique = true))
  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.CascadeType;
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.Table;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:关系被维护端
 */
@Entity
@Table(name = "t_idcard")
public class IdCard {
  @Id
  @GeneratedValue
  private Long id;
  @Column(length = 18)
  private String cardNo;
  @OneToOne(mappedBy = "idCard", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  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();
  }

}

其他关联项目

源码地址

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

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

posted @ 2016-12-16 17:53  JE_GE  阅读(345)  评论(0编辑  收藏  举报