JPA关联映射 - 一对一映射

1.配置文件

JPA规范要求在类路径的META-INF目录下放置persistence.xml


	
		
			
			
			
			
			
			
			
			
			
		
	

2.实体类
(1)一的一方Person(关系维护方)

package cn.itcast.domain;

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;

/**
 * Person用户实体类
 */
@Entity
public class Person {
	/** 主键 */
	private Integer id;
	/** 姓名 */
	private String name;
	/** 拥有身份证 */
	private IdCard idcard;

	public Person() {}

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

	@Id @GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(length = 10, nullable = false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne(cascade=CascadeType.ALL,optional=false)
	@JoinColumn(name="idcard_id")
	public IdCard getIdcard() {
		return idcard;
	}
	public void setIdcard(IdCard idcard) {
		this.idcard = idcard;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + "]";
	}
}
(2) 一的一方IDCard(关系被维护方)
package cn.itcast.domain;

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

/**
 * IdCard身份证实体类
 */
@Entity
public class IDCard {
	/** 主键 */
	private Integer id;
	/** 名称 */
	private String cardno;
	/** 所属公民 */
	private Person person;

	public IDCard() {}

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

	@Id @GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(length = 18, nullable = false, unique = true)
	public String getCardno() {
		return cardno;
	}
	public void setCardno(String cardno) {
		this.cardno = cardno;
	}
	@OneToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH}, mappedBy="idCard")
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	@Override
	public String toString() {
		return "IDCard [id=" + id + ", cardno=" + cardno + "]";
	}
}
3.一对一单元测试类PersonTest
package junit.test;

import java.util.List;

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

import org.junit.Test;

import cn.itcast.domain.IDCard;
import cn.itcast.domain.Person;

/**
 * 一对一单元测试类
 */
public class PersonTest {
	
	@Test
	public void testAddPerson(){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Person person = new Person("王少杰");
		IDCard idCard = new IDCard("142726198512290613");
		person.setIdCard(idCard);
		idCard.setPerson(person);
		em.persist(person);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}
	
	@Test
	public void testUpdatePerson(){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Person person = em.find(Person.class, 1);
		person.setName("孔艳娇111");
		IDCard idCard = person.getIdCard();
		idCard.setCardno("11111111111111");
		person.setIdCard(idCard);
		System.out.println(person);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}
	
	@Test
	public void testDeletePerson(){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Person person = em.getReference(Person.class, 1);
		em.remove(person);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}
	
	@Test
	public void testGetPersonById(){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		Person person = em.find(Person.class, 1);
		System.out.println(person);
		em.close();
		factory.close();
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testGetAllPerson(){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		Query query = em.createQuery("from Person o");
		List personList = query.getResultList();
		for (Person person : personList) {
			System.out.println("person:" + person);
			IDCard idCard = person.getIdCard();
			System.out.println("idCard: " + idCard);
		}
		em.close();
		factory.close();
	}
}


你可能感兴趣的:(JPA)