hibernate一对一唯一外键关联映射(双向关联Person<---->IdCard) public class Person { private int id; private String name; private IdCard idCard; -------------------------------------------------------------------------------------------- public class IdCard { private int id; private String cardNo; private Person person; -------------------------------------------------------------------------------------------- <hibernate-mapping> < class name="com.bjsxt.hibernate.IdCard" table="t_idcard"> <id name="id"> <generator class="native"/> </id> <property name="cardNo"/> <!-- 一对一唯一外键关联双向,需要在(idcard)端,添加one2one标签, 指示hibernate如何加载其关联对象,默认根据主键加载person, 此时,在外键关联映射中,因为两个实体采用的是person的外键维护的关系, 所以不能指定主键加载person,而要根据person的外键idCard加载,所以采用如下映射方式
--> <one-to-one name="person" property-ref="idCard"/> </ class> </hibernate-mapping> <hibernate-mapping> < class name="com.bjsxt.hibernate.Person" table="t_person"> <id name="id"> <generator class="native"/> </id> <property name="name"/> < many-to-one name="idCard" unique="true"/> </ class> </hibernate-mapping> -------------------------------------------------------------------------------------------- public void testSave() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); IdCard idCard = new IdCard(); idCard.setCardNo("88888888888888"); session.save(idCard); Person person = new Person(); person.setName("菜10"); person.setIdCard(idCard); 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); } } 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); } } -------------------------------------------------------------------------------------------- 一对一唯一外键关联双向,需要在另一端(idcard),添加<one-to-one>标签,指示hibernate如何加载 其关联对象,默认根据主键加载person,外键关联映射中,因为两个实体采用的是person的外键维护的关系, 所以不能指定主键加载person,而要根据person的外键加载,所以采用如下映射方式: <one-to-one name="person" property-ref="idCard"/> 由于没有改变数据库结构,不象many-to-one会往数据库中加字段,所以,我们还是使用的07hibernate_one2one_ufk_1,而 没有去重新导入 一个新的表和数据库08hibernate_one2one_ufk_2 -------------------------------------------------------------------------------------------- testLoad1: Hibernate: select person0_.id as id1_0_, person0_.name as name1_0_, person0_.idCard as idCard1_0_ from t_person person0_ where person0_.id=? person.name=菜10 Hibernate: select idcard0_.id as id0_1_, idcard0_.cardNo as cardNo0_1_, person1_.id as id1_0_, person1_.name as name1_0_, person1_.idCard as idCard1_0_ from t_idcard idcard0_ left outer join t_person person1_ on idcard0_.id=person1_.idCard where idcard0_.id=? Hibernate: select person0_.id as id1_0_, person0_.name as name1_0_, person0_.idCard as idCard1_0_ from t_person person0_ where person0_.idCard=? idCard.cardNo=88888888888888 testLoad2: Hibernate: select idcard0_.id as id0_1_, idcard0_.cardNo as cardNo0_1_, person1_.id as id1_0_, person1_.name as name1_0_, person1_.idCard as idCard1_0_ from t_idcard idcard0_ left outer join t_person person1_ on idcard0_.id=person1_.idCard where idcard0_.id=? idcard.cardNo=88888888888888 idcard.person.name=菜10
--> <one-to-one name="person" property-ref="idCard"/> </ class> </hibernate-mapping> <hibernate-mapping> < class name="com.bjsxt.hibernate.Person" table="t_person"> <id name="id"> <generator class="native"/> </id> <property name="name"/> < many-to-one name="idCard" unique="true"/> </ class> </hibernate-mapping> -------------------------------------------------------------------------------------------- public void testSave() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); IdCard idCard = new IdCard(); idCard.setCardNo("88888888888888"); session.save(idCard); Person person = new Person(); person.setName("菜10"); person.setIdCard(idCard); 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); } } 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); } } -------------------------------------------------------------------------------------------- 一对一唯一外键关联双向,需要在另一端(idcard),添加<one-to-one>标签,指示hibernate如何加载 其关联对象,默认根据主键加载person,外键关联映射中,因为两个实体采用的是person的外键维护的关系, 所以不能指定主键加载person,而要根据person的外键加载,所以采用如下映射方式: <one-to-one name="person" property-ref="idCard"/> 由于没有改变数据库结构,不象many-to-one会往数据库中加字段,所以,我们还是使用的07hibernate_one2one_ufk_1,而 没有去重新导入 一个新的表和数据库08hibernate_one2one_ufk_2 -------------------------------------------------------------------------------------------- testLoad1: Hibernate: select person0_.id as id1_0_, person0_.name as name1_0_, person0_.idCard as idCard1_0_ from t_person person0_ where person0_.id=? person.name=菜10 Hibernate: select idcard0_.id as id0_1_, idcard0_.cardNo as cardNo0_1_, person1_.id as id1_0_, person1_.name as name1_0_, person1_.idCard as idCard1_0_ from t_idcard idcard0_ left outer join t_person person1_ on idcard0_.id=person1_.idCard where idcard0_.id=? Hibernate: select person0_.id as id1_0_, person0_.name as name1_0_, person0_.idCard as idCard1_0_ from t_person person0_ where person0_.idCard=? idCard.cardNo=88888888888888 testLoad2: Hibernate: select idcard0_.id as id0_1_, idcard0_.cardNo as cardNo0_1_, person1_.id as id1_0_, person1_.name as name1_0_, person1_.idCard as idCard1_0_ from t_idcard idcard0_ left outer join t_person person1_ on idcard0_.id=person1_.idCard where idcard0_.id=? idcard.cardNo=88888888888888 idcard.person.name=菜10