Hibernate 설정 파일 기반 (12) 1 대 1 양 방향 연결 맵

13452 단어 Hibernate
일대일 관련 관 계 는 두 대상 을 독립 적 으로 대해 야 할 때 두 가지 유형 으로 나 누 어 매 핑 을 완성 한다.
 
홈 키 연결 맵, 추가 필드 유지 보수 관련 없 음, id 값 일치
package org.leadfar.hibernate.model;


public class Person {
	private long id;
	private String name;
	private IdCard idcard;
	
	Person(){}
	
	public long getId() {
		return id;
	}
	public IdCard getIdcard() {
		return idcard;
	}

	public String getName() {
		return name;
	}
	public void setId(long id) {
		this.id = id;
	}
	public void setIdcard(IdCard idcard) {
		this.idcard = idcard;
	}
	public void setName(String name) {
		this.name = name;
	}

	
}

 
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
	package="org.hibernate.auction">
	<class name="org.leadfar.hibernate.model.Person" table="t_persons" >
		<!-- id      ,     -->
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<!--     id t_idcard   id         -->
		<one-to-one name="idcard"> </one-to-one>
		
	</class>
	
</hibernate-mapping>

 
package org.leadfar.hibernate.model;


public class IdCard {
	private long id;
	private String sn;
    private Person person;
    
    IdCard() {}
	public long getId() {
		return id;
	}
	public Person getPerson() {
		return person;
	}
	
	
	public String getSn() {
		return sn;
	}
	public void setId(long id) {
		this.id = id;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public void setSn(String sn) {
		this.sn = sn;
	}

}

 
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
	package="org.hibernate.auction">
	<class name="org.leadfar.hibernate.model.IdCard" table="t_idcard" >
		
		<id name="id">
			<!--   foreign      id       id  -->
			<generator class="foreign">
			<!--         (               ,    id) -->
			<param name="property">person</param>
			</generator>
		</id>
		<property name="sn" not-null="true" unique="true"></property>
		<!--constraint          , idcard          person -->
		<!-- one-to-one       id         id     -->
		<one-to-one name="person" constrained="true"></one-to-one>
		
	</class>
	
</hibernate-mapping>

 
테스트
package org.leadfar.hibernate.model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import junit.framework.TestCase;

public class OneToOneTest extends TestCase {
	
	public void oneToOne_save() {
		Configuration cfg = new Configuration().configure();
		SessionFactory sf = cfg.buildSessionFactory();
		Session session = sf.openSession();
		
		try {
			session.beginTransaction();
			/**
			 *        :one-to-one idCard id     foreign,  Person id        id(  id    )
			 *  Person,  IdCard,  IdCard id        Person  id
			 *  ,     Person,   IdCard
			 */
			
			Person p1 = new Person();
			p1.setName("  1");
			Person p2 = new Person();
			p2.setName("  2");
			Person p3 = new Person();
			p3.setName("  3");
			session.save(p1);
			session.save(p2);
			session.save(p3);
			
			
			IdCard idcard = new IdCard();
			idcard.setSn("21938404320997903");
			idcard.setPerson(p3);//  p3         id,    idcard   id
			
			session.save(idcard);
			
			
			session.getTransaction().commit();
		} catch(Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	
	//  Person  idCard
	public void oneToOne_load01() {
		Configuration cfg = new Configuration().configure();
		SessionFactory sf = cfg.buildSessionFactory();
		Session session = sf.openSession();
		
		try {
			session.beginTransaction();
			
			Person p1 = (Person)session.load(Person.class, 3L);//  id long  ,    L  
			IdCard idcard = p1.getIdcard();
			System.out.println(p1.getName()+","+idcard.getSn());
			
			session.getTransaction().commit();
		} catch(Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	//  idCard  Person
	public void oneToOne_load02() {
		Configuration cfg = new Configuration().configure();
		SessionFactory sf = cfg.buildSessionFactory();
		Session session = sf.openSession();
		
		try {
			session.beginTransaction();
			
			IdCard idcard = (IdCard)session.load(IdCard.class, 3L);
			Person person = idcard.getPerson();
			System.out.println(idcard.getSn()+","+person.getName());
			
			session.getTransaction().commit();
		} catch(Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
}

 
 
유일한 외부 키 연결 맵, 그 중 한 끝 에 1 개의 필드 를 사용 하여 상대방 의 id 를 기록 합 니 다.
package org.leadfar.hibernate.model;


public class Person {
	private long id;
	private String name;
	private IdCard idcard;
	
	Person(){}
	
	public long getId() {
		return id;
	}
	public IdCard getIdcard() {
		return idcard;
	}

	public String getName() {
		return name;
	}
	public void setId(long id) {
		this.id = id;
	}
	public void setIdcard(IdCard idcard) {
		this.idcard = idcard;
	}
	public void setName(String name) {
		this.name = name;
	}

	
}

 
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
	package="org.hibernate.auction">
	<class name="org.leadfar.hibernate.model.Person" table="t_persons" >
		<!--      ,id        ,      id      -->
		<id name="id">
			<generator class="assigned">
			</generator>
		</id>
		<property name="name"/>
		<!-- property-ref="person"             person       ,          id    !-->
		<!--      property-ref="person"        id      ,           !      !!-->
		<one-to-one name="idcard" property-ref="person"> </one-to-one>
		
	</class>
	
</hibernate-mapping>

 
package org.leadfar.hibernate.model;


public class IdCard {
	private long id;
	private String sn;
    private Person person;
    
    IdCard() {}
	public long getId() {
		return id;
	}
	public Person getPerson() {
		return person;
	}
	
	
	public String getSn() {
		return sn;
	}
	public void setId(long id) {
		this.id = id;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public void setSn(String sn) {
		this.sn = sn;
	}

}

 
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
	package="org.hibernate.auction">
	<class name="org.leadfar.hibernate.model.IdCard" table="t_idcard" >
		<!--      ,id        ,      id      -->
		<id name="id">
			<generator class="assigned"/>
		</id>
		<property name="sn" not-null="true" unique="true"></property>
		<!-- unique=true  many-to-one          -->
		<!--      many-to-one    , person           -->
		<!--     ,IdCard     person     Person -->
		<!--   ,Person       person         ,     property-ref=person -->
		<!--            person         -->
		<many-to-one name="person" unique="true" column="personid"></many-to-one>
		
	</class>
	
</hibernate-mapping>

 
테스트
package org.leadfar.hibernate.model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import junit.framework.TestCase;

public class OneToOneTest extends TestCase {
	public void oneToOne_save() {
		Configuration cfg = new Configuration().configure();
		SessionFactory sf = cfg.buildSessionFactory();
		Session session = sf.openSession();
		
		try {
			session.beginTransaction();
			/**
			 *one-to-one        
			 *1.   Person (one-to-one)Idcard  person       person    id
			 *<one-to-one name="idcard" property-ref="person"> </one-to-one>
			 *
			 *2.   IdCard,  IdCard  person          Person   (many-to-one)
			 *<many-to-one name="person" unique="true" column="personid"></many-to-one>
			 */
			
			Person p1= new Person();
			p1.setId(1L);
			p1.setName("1abc");
			session.save(p1);
			
			Person p2= new Person();
			p2.setId(2L);
			p2.setName("2abc");
			session.save(p2);
			
			Person p3= new Person();
			p3.setId(3L);
			p3.setName("3abc");
			session.save(p3);
			
			
			IdCard idcard1 = new IdCard();
			idcard1.setId(2L);
			idcard1.setSn("11111111111");
			idcard1.setPerson(p1);//         
			session.save(idcard1);
			
			IdCard idcard2 = new IdCard();
			idcard2.setId(3L);
			idcard2.setSn("22222222222");
			idcard2.setPerson(p2);//         
			session.save(idcard2);
			
			IdCard idcard3 = new IdCard();
			idcard3.setId(4L);
			idcard3.setSn("33333333333");
			idcard3.setPerson(p3);//         
			session.save(idcard3);
			
			session.getTransaction().commit();
		} catch(Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	
	//  Person  idCard
	public void oneToOne_load01() {
		Configuration cfg = new Configuration().configure();
		SessionFactory sf = cfg.buildSessionFactory();
		Session session = sf.openSession();
		
		try {
			session.beginTransaction();
			
			Person p1 = (Person)session.load(Person.class, 2L);//  id long  ,    L  
			IdCard idcard = p1.getIdcard();
			System.out.println(p1.getName()+","+idcard.getSn());
			
			session.getTransaction().commit();
		} catch(Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	//  idCard  Person
	public void oneToOne_load02() {
		Configuration cfg = new Configuration().configure();
		SessionFactory sf = cfg.buildSessionFactory();
		Session session = sf.openSession();
		
		try {
			session.beginTransaction();
			
			IdCard idcard = (IdCard)session.load(IdCard.class, 2L);
			Person person = idcard.getPerson();
			System.out.println(idcard.getSn()+","+person.getName());
			
			session.getTransaction().commit();
		} catch(Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
}

 
 

좋은 웹페이지 즐겨찾기