zoukankan      html  css  js  c++  java
  • 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

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

  • 相关阅读:
    Message高级特性 & 内嵌Jetty实现文件服务器
    springboot中使用kindeditor富文本编辑器实现博客功能&vue-elementui使用vue-kindeditor
    Embarcadero RAD Studio XE5
    经典营销故事
    百度竞价教程 借助百度热力图让你的效果翻10倍
    无本借力:他是如何实现年收入70万?
    不用软件快速拥有几百个QQ群并都是管理员
    质保、保修、包修:含义不同
    域名反向解析在自建邮件群发服务器中的应用
    2014年1月1日,马年
  • 原文地址:https://www.cnblogs.com/je-ge/p/6193083.html
Copyright © 2011-2022 走看看