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("88888888888888"); 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, 1); 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); } } public void testLoad2() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); IdCard idCard = (IdCard)session.load(IdCard. class, 1); System.out.println("idcard.cardNo=" + idCard.getCardNo()); System.out.println("idcard.person.name=" + idCard.getPerson().getName()); session.getTransaction().commit(); } catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } } } -------------------------------------------------- hibernate一对一主键关联映射(双向关联Person<---->IdCard) // 需要在idcard映射文件中加入<one-to-one>标签指向person:<one-to-one name="person"/>, // 指示hibernate如何加载person默认根据主键加载 <hibernate-mapping> < class name="com.bjsxt.hibernate.IdCard" table="t_idcard"> <id name="id"> <generator class="native"/> </id> <property name="cardNo"/> <one-to-one name="person"/> </ class> </hibernate-mapping> 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.14 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.00 sec) TestSave: Hibernate: insert into t_idcard (cardNo) values (?) Hibernate: insert into t_person (name, id) values (?, ?) TestLoaded1: Hibernate: select person0_.id as id1_0_, person0_.name as name1_0_ from t_person person0_ where person0_.id=? person.name=孙悟空 Hibernate: select idcard0_.id as id0_1_, idcard0_.cardNo as cardNo0_1_, person1_.id as id1_0_, person1_.name as name1_0_ from t_idcard idcard0_ left outer join t_person person1_ on idcard0_.id=person1_.id where idcard0_.id=? idCard.cardNo=88888888888888 TestLoaded2: Hibernate: select idcard0_.id as id0_1_, idcard0_.cardNo as cardNo0_1_, person1_.id as id1_0_, person1_.name as name1_0_ from t_idcard idcard0_ left outer join t_person person1_ on idcard0_.id=person1_.id where idcard0_.id=? idcard.cardNo=88888888888888 idcard.person.name=孙悟空