JPA関連マッピング-1対1マッピング

6115 ワード

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();
	}
}