JPA 菜鸟教程 10 双向一对一关联表
来源:互联网 发布:java socket框架 mina 编辑:程序博客网 时间:2024/06/05 07:01
GitHub
双向一对一关联表的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(); }}
其他关联项目
- JPA 菜鸟教程 8 双向一对一共享主键
http://blog.csdn.net/je_ge/article/details/53495313 - JPA 菜鸟教程 9 双向一对一唯一外键
http://blog.csdn.net/je_ge/article/details/53495501
源码地址
https://github.com/je-ge/jpa
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!
0 0
- JPA 菜鸟教程 10 双向一对一关联表
- JPA 一对一双向关联
- JPA 菜鸟教程 8 双向一对一共享主键
- JPA 菜鸟教程 9 双向一对一唯一外键
- JPA一对一双向关联实例
- jpa双向一对一关联关系
- JPA之JPA中的双向一对一关联
- JPA开发总结<四>--一对一双向关联
- JPA映射关联关系-双向一对一
- 【JPA】@OneToOne 一对一双向关联注解
- JPA的双向一对多和双向一对一关联关系
- JPA学习笔记(10)——映射双向一对一关联关系
- JPA学习笔记(10)——映射双向一对一关联关系
- JPA学习笔记-映射双向一对一的关联关系
- JPA 菜鸟教程 5 双向一对多
- JPA--双向一对一
- JPA:双向一对一
- Hibernate 一对一连接表双向关联
- Hibernate中Session的操作解释
- Python_Day_2
- Button的三种事件总结
- Xcode8(包括Application Loader)上传AppStore 无法构建版本 没有➕号
- 16 - 12 - 07 迪杰斯特拉(Dijkstra)算法-最短路径 - の わもがたり
- JPA 菜鸟教程 10 双向一对一关联表
- 结构体的简单理解
- udp
- Hello,fish!
- 第14周OJ实践 矩阵相加
- Maven系列(三)Maven给不同的环境打包
- RTMPdump源码分析: NetStream1(6)
- HttpGet与HttpPost区别
- 评估假设