jpa一对一映射案例

 双向和单向的区别,其实不在数据库,而在加载方向。如Person和IdCard,单向是Person拥有IdCard的实例可以加载IdCard,而双向不但Person可以加载IdCard,IdCard可以拥有Person实例来加载Person。数据库还是原来的数据库(两个共享主键的表)

以下模拟双向一对一得映射案例

表结构如下:

Person表结构

jpa一对一映射案例

IdCard表结构

 jpa一对一映射案例

IDCard类

  
    
package com.ljq.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
/**
* 身份证
*
*
@author jiqinlin
*
*/
@Entity
@Table(name
= " tb_idcard " )
public class IDCard {
@Id
@GeneratedValue
private Integer id;

@Column(nullable
= false , length = 18 )
private String cardno;

// unique= true 指明personid列的值不可重复。
// optional = false指明Person不可为空
@OneToOne(cascade = CascadeType.ALL, optional = false )
@JoinColumn(name
= " personid " ,referencedColumnName = " id " , unique = true )
private Person person;

public IDCard() {
super ();
}

public IDCard(String cardno) {
super ();
this .cardno = cardno;
}


public String getCardno() {
return cardno;
}

public void setCardno(String cardno) {
this .cardno = cardno;
}


public Integer getId() {
return id;
}

public void setId(Integer id) {
this .id = id;
}


public Person getPerson() {
return person;
}

public void setPerson(Person person) {
this .person = person;
}

}

Person类

  
    
package com.ljq.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
* 人
*
*
@author jiqinlin
*
*/
@Entity
@Table(name
= " tb_person " )
public class Person {
@Id
@GeneratedValue
private Integer id;

@Column(nullable
= false , length = 20 )
private String name;

// optional = false指明IDCard可为空
// mappedBy="person"指明Person作为双向关系的维护段,负责外键的更新,起主导作用
@OneToOne(optional = true , cascade = CascadeType.ALL, mappedBy = " person " )
private IDCard idcard;

public Person() {
super ();
}

public Person(String name) {
super ();
this .name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer 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;
}

}

OneToOneTest测试类

  
    
package com.ljq.test;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.Test;

import com.ljq.entity.IDCard;
import com.ljq.entity.Person;

public class OneToOneTest {

/**
* 添加人的时候同时添加对应的身份证
*/
@Test
public void save() {
EntityManagerFactory factory
= Persistence.createEntityManagerFactory( " ljq " );
EntityManager em
= factory.createEntityManager();
em.getTransaction().begin();

Person person
= new Person();
person.setName(
" lisi " );
IDCard idcard
= new IDCard();
idcard.setCardno(
" 350524 " );
idcard.setPerson(person);
person.setIdcard(idcard);

em.persist(person);

em.getTransaction().commit();
em.close();
factory.close();
}

/**
* 删除
*/
@Test
public void delete() {
EntityManagerFactory factory
= Persistence.createEntityManagerFactory( " ljq " );
EntityManager em
= factory.createEntityManager();
em.getTransaction().begin();

// 删除人同时会自动删除身份证
em.remove(em.getReference(Person. class , 2 ));

// 删除身份证同时删除人
// em.remove(em.getReference(IDCard.class, (Serializable)1));

em.getTransaction().commit();
em.close();
factory.close();
}

/**
* 更新
*/
@SuppressWarnings(
" unchecked " )
@Test
public void update() {
EntityManagerFactory factory
= Persistence.createEntityManagerFactory( " ljq " );
EntityManager em
= factory.createEntityManager();
em.getTransaction().begin();

List
< Person > persons = em.createQuery( " select o from Person o " ).getResultList();
for (Person person:persons){
person.setName(
" wangwu " );
IDCard idcard
= person.getIdcard();
if (idcard != null ){
idcard.setCardno(
" abc " );
}
em.merge(person);
}

em.getTransaction().commit();
em.close();
factory.close();
}

/**
* 用来判断映射是否成功
*
*/
@Test
public void test() {
Persistence.createEntityManagerFactory(
" ljq " );
}

}

你可能感兴趣的:(jpa)