一对一双向关联,在一对一的单向的基础上,在不维护关系的那一端添加one-to-one配置
实体类和配置如下
package domain;
public class Card {
private int id;
private String no;
private Person person;
//get/set和构造省略,但实际不可省略
}
1 <?xml version="1.0"?>
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4 <!-- Generated 2014-7-9 7:00:47 by Hibernate Tools 3.4.0.CR1 -->
5 <hibernate-mapping>
6 <class name="domain.Card" table="CARD">
7 <id name="id" type="int">
8 <column name="ID" />
9 <generator class="native" />
10 </id>
11 <property name="no" type="java.lang.String">
12 <column name="NO" />
13 </property>
14 <many-to-one column="pid" name="person" class="domain.Person" unique="true"/>
15 </class>
16 </hibernate-mapping>
17
package domain;
public class Person {
private int id;
private String name;
private Card card;
//get/set和构造省略,但实际不可省略
}
1 <?xml version="1.0"?>
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4 <!-- Generated 2014-7-9 7:00:47 by Hibernate Tools 3.4.0.CR1 -->
5 <hibernate-mapping>
6 <class name="domain.Person" table="PERSON">
7 <id name="id" type="int">
8 <column name="ID" />
9 <generator class="native" />
10 </id>
11 <property name="name" type="java.lang.String">
12 <column name="NAME" />
13 </property>
14 <one-to-one name="card" class="domain.Card" property-ref="person"/>
15 </class>
16 </hibernate-mapping>
17
书写测试类
1 package demo;
2
3 import org.hibernate.Session;
4 import org.junit.Test;
5
6 import domain.Card;
7 import domain.Person;
8 import util.HibernateUtil;
9
10 public class App
11 {
12 @Test
13 /**
14 * 一对一双向,关系由card的控制
15 */
16 public void test01(){
17
18 Session session = null;
19 try {
20 session = HibernateUtil.openSession();
21 session.beginTransaction();
22
23 Person person = new Person();
24 person.setName("Nick");
25 session.save(person);
26
27 Card card = new Card();
28 card.setNo("#1");
29 card.setPerson(person);
30
31 session.save(card);
32
33 session.getTransaction().commit();
34
35 } catch (Exception e) {
36 e.printStackTrace();
37 if (session!=null) {
38 session.getTransaction().rollback();
39 }
40 }finally{
41 if (session!=null) {
42 session.close();
43 }
44 }
45
46 }
47
48 @Test
49 /**
50 * 一对一双向,关系由card的控制,重复添加多个card给一个person会报外键约束的异常
51 */
52 public void test02(){
53
54 Session session = null;
55 try {
56 session = HibernateUtil.openSession();
57 session.beginTransaction();
58
59 Person person = (Person) session.load(Person.class, 1);
60
61 Card card = new Card();
62 card.setNo("#8");
63 card.setPerson(person);
64
65 session.save(card);
66
67 session.getTransaction().commit();
68
69 } catch (Exception e) {
70 e.printStackTrace();
71 if (session!=null) {
72 session.getTransaction().rollback();
73 }
74 }finally{
75 if (session!=null) {
76 session.close();
77 }
78 }
79
80 }
81
82 @Test
83 /**
84 * 一对一双向关联,需要使用load方法取不维护关系的一端,会直接取得关联的一端,不使用懒加载
85 * 因为不维护关系,也就不存在外键,不存在外键也就不存在懒加载,一对一双向关联在实际开发中
86 * 不建议使用,因为查询其中一方,如果设置不当,会产生嵌套查询
87 */
88 public void test03(){
89
90 Session session = null;
91 try {
92 session = HibernateUtil.openSession();
93 session.beginTransaction();
94
95 Person person = (Person) session.load(Person.class, 1);
96
97 System.out.println(person.getName());
98 System.out.println(person.getCard().getNo());
99
100 session.getTransaction().commit();
101
102 } catch (Exception e) {
103 e.printStackTrace();
104 if (session!=null) {
105 session.getTransaction().rollback();
106 }
107 }finally{
108 if (session!=null) {
109 session.close();
110 }
111 }
112 }
113
114 @Test
115 /**
116 * 一对一双向关联,使用load方法取维护关系的一端,不会直接取得关联的一端,使用懒加载
117 * 但是一旦取得不维护关系的一端,会发现不维护关系的一端关联另一个实体,会直接查询,这样
118 * 会导致冗余操作
119 */
120 public void test04(){
121
122 Session session = null;
123 try {
124 session = HibernateUtil.openSession();
125 session.beginTransaction();
126
127 Card card = (Card) session.load(Card.class, 1);
128
129 System.out.println(card.getNo());
130 System.out.println(card.getPerson().getName());
131
132 session.getTransaction().commit();
133
134 } catch (Exception e) {
135 e.printStackTrace();
136 if (session!=null) {
137 session.getTransaction().rollback();
138 }
139 }finally{
140 if (session!=null) {
141 session.close();
142 }
143 }
144 }
145
146 }
147