05hibernate_one2one_pk_1:
package com.bjsxt.hibernate;
import org.hibernate.Session;
import junit.framework.TestCase;
public class One2OneTest extends TestCase {
public void testSave1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
IdCard idCard = new IdCard();
idCard.setCardNo("9999999999999");
Person person = new Person();
person.setName("七仙女");
person.setIdCard(idCard);
//不会出现TransientObjectException异常
//因为一对一主键关联映射中,默认了cascade属性
session.save(person);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testLoad1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Person person = (Person)session.load(Person.class, 2);
System.out.println("person.name=" + person.getName());
System.out.println("idCard.cardNo=" + person.getIdCard().getCardNo());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
-----------------------------------------------------
hibernate一对一主键关联映射(单向关联Person---->IdCard)
一对一主键关联映射:让两个实体对象的id保持相同,这样可以避免多余的字段被创建
具体映射:
a)主表:IdCard.hbm.xml
<hibernate-mapping>
<class name="com.bjsxt.hibernate.IdCard" table="t_idcard">
<id name="id">
<!--主表,采用自增native-->
<generator class="native"/>
</id>
<property name="cardNo"/>
</class>
</hibernate-mapping>
b)从表:Person.hbm.xml
<id name="id">
<!-- person的主键来源idCard,也就是共享idCard的主键 -->
<!--从表,采用自增foreign,参考主表IdCare中的主键-->
<generator class="foreign">
<param name="property">idCard</param>
</generator>
</id>
<property name="name"/>
<!-- one-to-one标签的含义,指示hibernate怎么加载它的关联对象,默认根据主键加载,
constrained="true", 表明当前主键上存在一个约束,person的主键作为外键参照了idCard
-->
<one-to-one name="idCard" constrained="true"/>
生成的表:
create table t_idcard (id integer not null auto_increment, cardNo varchar(255), primary key (id))
create table t_person (id integer not null, name varchar(255), primary key (id))
alter table t_person add index FK785BED809AC0D5D6 (id), add constraint FK785BED809AC0D5D6 foreign key (id) references t_idcard (id)
mysql> desc t_idcard;
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| cardNo | varchar(255) | YES | | NULL | |
+--------+--------------+------+-----+---------+----------------+
2 rows in set (0.05 sec)
mysql> desc t_person;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
TestLoad:
Hibernate: insert into t_idcard (cardNo) values (?)
Hibernate: insert into t_person (name, id) values (?, ?)
package com.bjsxt.hibernate;
import org.hibernate.Session;
import junit.framework.TestCase;
public class One2OneTest extends TestCase {
public void testSave1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
IdCard idCard = new IdCard();
idCard.setCardNo("9999999999999");
Person person = new Person();
person.setName("七仙女");
person.setIdCard(idCard);
//不会出现TransientObjectException异常
//因为一对一主键关联映射中,默认了cascade属性
session.save(person);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testLoad1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Person person = (Person)session.load(Person.class, 2);
System.out.println("person.name=" + person.getName());
System.out.println("idCard.cardNo=" + person.getIdCard().getCardNo());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
-----------------------------------------------------
hibernate一对一主键关联映射(单向关联Person---->IdCard)
一对一主键关联映射:让两个实体对象的id保持相同,这样可以避免多余的字段被创建
具体映射:
a)主表:IdCard.hbm.xml
<hibernate-mapping>
<class name="com.bjsxt.hibernate.IdCard" table="t_idcard">
<id name="id">
<!--主表,采用自增native-->
<generator class="native"/>
</id>
<property name="cardNo"/>
</class>
</hibernate-mapping>
b)从表:Person.hbm.xml
<id name="id">
<!-- person的主键来源idCard,也就是共享idCard的主键 -->
<!--从表,采用自增foreign,参考主表IdCare中的主键-->
<generator class="foreign">
<param name="property">idCard</param>
</generator>
</id>
<property name="name"/>
<!-- one-to-one标签的含义,指示hibernate怎么加载它的关联对象,默认根据主键加载,
constrained="true", 表明当前主键上存在一个约束,person的主键作为外键参照了idCard
-->
<one-to-one name="idCard" constrained="true"/>
生成的表:
create table t_idcard (id integer not null auto_increment, cardNo varchar(255), primary key (id))
create table t_person (id integer not null, name varchar(255), primary key (id))
alter table t_person add index FK785BED809AC0D5D6 (id), add constraint FK785BED809AC0D5D6 foreign key (id) references t_idcard (id)
mysql> desc t_idcard;
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| cardNo | varchar(255) | YES | | NULL | |
+--------+--------------+------+-----+---------+----------------+
2 rows in set (0.05 sec)
mysql> desc t_person;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
TestLoad:
Hibernate: insert into t_idcard (cardNo) values (?)
Hibernate: insert into t_person (name, id) values (?, ?)