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();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[JPA] 즉시로딩(EAGER)과 지연로딩(LAZY) (왜 LAZY 로딩을 써야할까?) (1)Proxy는 이 글의 주제인 즉시로딩과 지연로딩을 구현하는데 중요한 개념인데, 일단 원리는 미뤄두고 즉시로딩과 지연로딩이 무엇인지에 대해 먼저 알아보자. 눈 여겨 볼 곳은 'fetch = FetchType.EAGER...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.