hibernate 一对一双向 查询从对象通过关联查询怎么发出三条查询语句?

chenbing 发布于 2012/08/30 15:23
阅读 1K+
收藏 0

person.xml

<?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>
<class name="com.bjpowernode.hibernate.Person" table="t_person">
 <id name="id">
  <generator class="native">
  </generator>
 </id>
 <property name="name"></property>
 <many-to-one name="idCard"  unique="true"></many-to-one>
 </class>
</hibernate-mapping>

IdCard.hbm.xml

<?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>
<class name="com.bjpowernode.hibernate.IdCard" table="t_idCard">
 <id name="id">
  <generator class="native"></generator>
 </id>
 <property name="name"></property>
 <one-to-one name="person" property-ref="idCard"></one-to-one>
</class>
</hibernate-mapping>

One2OneTest.java

public void testLoad(){
  Session session=null;
  session=HibernateUtil.getSession();
  Transaction tx=null;
  try{
   tx=session.beginTransaction();
   Person p= (Person) session.get(Person.class, 1);
   System.out.println(p.getIdCard().getName());
   tx.commit();
  }catch(Exception e){
   if(tx!=null){
    tx.rollback();
   }
  }finally{
   HibernateUtil.closeSession(session);
  }
 }

控制台输出:

Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_.idCard as idCard0_0_ from t_person person0_ where person0_.id=?
Hibernate: select idcard0_.id as id1_1_, idcard0_.name as name1_1_, person1_.id as id0_0_, person1_.name as name0_0_, person1_.idCard as idCard0_0_ from t_idCard idcard0_ left outer join t_person person1_ on idcard0_.id=person1_.idCard where idcard0_.id=?
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_, person0_.idCard as idCard0_0_ from t_person person0_ where person0_.idCard=?
00010232

 

以下是问题补充:

@chenbing:如果配置单向发出两条查询语句这个能理解, (2012/08/30 15:26)
加载中
返回顶部
顶部