双向一对一共享主键的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. wamp配置sendmail发送邮件

    下载 sendmail ( 地址: http://www.glob.com.au/sendmail/sendmail.zip ) [PHP.ini 配置] [mail function]; For W ...

  2. Mysql授权远程登录

    在命令行输入如下命令即可: Grant all privileges on *.* to ' with grant option; 再执行 flush privileges

  3. 4、Web应用程序中的安全向量 -- over-posting(重复提交)

    模型绑定是ASP.NET MVC提供的强大功能,可遵照命名约定将输入元素映射到模型属性,从而极大地简化了处理用户输入的过程,然而,这也成为了攻击的另一种没接,给攻击者提供了一个填充模型属性的机会,右下 ...

  4. [SOJ] 1282. Computer games (KMP)

    坑爹题 1282. Computer Game Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Brian is an ...

  5. MySQL(3)-索引

    一.索引类型 在MySQL中,存储引擎使用索引,首先在索引中找到对应值,然后根据匹配的索引记录中找到对应的行. 无论是多么复杂的ORM工具,在精妙和复杂的索引面前都是"浮云".这里 ...

  6. Visual Studio 2010/2013 UTF8编码调试时显示中文

    VisualStudio 2010 SP1环境 1.设置string默认编码为utf8,只需要在文件头部加入以下代码 #pragma execution_character_set("utf ...

  7. 38.利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用comp

    //接口Compute package jieKou; public interface Compute { int Computer(int n,int m); } //加 package jieK ...

  8. .h 与 .hpp 文件

    hpp,顾名思义等于.h加上.cpp,在boost.Xerces等开源库中频繁出现,偶在机缘巧合之下,学得一招半式,遂记录如下,以供参考学习.     hpp,其实质就是将.cpp的实现代码混入.h头 ...

  9. ACPI

    高级配置与电源接口(Advanced Configuration and Power Interface),简称ACPI.1997年由Intel.Microsoft.Toshiba 所共同制定提供操作 ...

  10. selinux策略开发

    所用软件: 1.setools -->seaudit  读取日志以确定所需权限 2.Reference Policy  -->  https://github.com/TresysTech ...