Hibernate条件createalias方法生成额外的select查询

时间:2016-08-04 04:30:45

标签: hibernate hibernate-mapping hibernate-criteria

Hibernate createalias()方法用于创建连接。当我调用s.iterator()方法时,hibernate会生成另一个select查询来获取子记录吗?根据我的理解,它生成单选择查询以使用join获取父记录和子记录。

Criteria ct = sn.createCriteria(Vendor.class);      
Criteria ct1= ct.createAlias("children", "cust");   
List<Vendor> l = ct.list();
Iterator<Vendor> i = l.iterator();
while (i.hasNext()) {
    Vendor v = (Vendor) i.next();
    System.out.println("name " + v.getVendorname());
    Set<Customer> s = v.getChildren();
    Iterator<Customer> it = s.iterator();
    while (it.hasNext()) {
        Customer c = it.next();
        System.out.println("custname " + c.getCustomername());
    }
}

映射文件:

<hibernate-mapping>
    <class name="com.criteria.example.Customer" table="customer">
        <id name="customerid" column="cid">
            <generator class="native" />
        </id>
        <property name="customername" type="string">
            <column name="cname" length="10" not-null="true" />
        </property>
    </class>
</hibernate-mapping>
<hibernate-mapping>
    <class name="com.criteria.example.Vendor" table="vendor">
        <id name="vendorid" column="vid">
            <generator class="native" />
        </id>
        <property name="vendorname" type="string">
            <column name="vname" length="10" not-null="true" />
        </property>
        <set name="children" inverse="false" cascade="all" lazy="false">
            <key column="vendid" not-null="true" />
            <one-to-many class="com.criteria.example.Customer" />
        </set>
    </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:1)

经过谷歌搜索后,我终于得到了这个问题的答案。 生成其他查询是hibernate中的一个错误。