我的两张表是一对一的关系,进行添加,修改操作时都很正常,但是使用hibernate对主表进行查询时,总是带出关联表的数据,这是为什么???public class Product implements Serializable{ private String id;
private String name;
private ProductBill productBill;
}public class ProductBill implements Serializable{

private String id;
private Product product;
private int count;
private int blockCount;
} <class name="Product">
<id name="id" length="20">
<generator class="cn.com.hch.base.hibernate.util.IdGenerator"/>
</id>
<property name="name" length="50" not-null="true" unique="true"/>
<one-to-one name="productBill" cascade="save-update"/>
</class> <class name="ProductBill">
<id name="id" length="20">
<generator class="foreign">
<param name="property">product</param>
</generator>
</id>
<one-to-one name="product" constrained="true"/>
<property name="count"/>
<property name="blockCount"/>
</class>这是查询时使用的方法
public List<Entity> getListAll(){
return this.getSession().createCriteria(getEntityClass()).list();
}
查询结果的数据格式为:
list->object(2){productbill,product}.......
其它的单表查询都是list里面直接就是bean对象,可是这个一对一的怎么里面有两个对象,productbill对象怎么不查询出来呢??求高手指点

解决方案 »

  1.   

    一对一默认就会加载关联对象的。
    因为一对一,hibernate认为它是一个属性,这种情况你可以使用hibernate instrument对单个字段进行lazy load试下。
    至少,我碰到的项目,一对一,它都是会自动加载关联对象的。 
      

  2.   


    如果关联对象productbill在product里面被加载我可以理解,可是现在我对product进行查询时,在list里面productbill与product同时存在的,这个我有些糊涂了
      

  3.   

    我把productBill这个属性改成懒加载的list中还是同时有两个bean对象,product里面的productbill属性发生了变化了
      

  4.   

    上面的图片是查询出来的效果。看不到的话这是图片的地址:http://fmn.rrimg.com/fmn046/20110525/1730/p_large_375z_726e000059675c71.jpg
      

  5.   

    用lazy加载 这样你如果真的不用到另外个表的数据 它是不会读出来的