我与Vendor
与Customer
的关系one-to-many。当我使用条件执行查询并FetchMode.JOIN
时,我应该获得一个选择查询,但我会收到8个选择查询(1 Vendor
+ 8 Customer
)。
代码
Criteria criteria = session.createCriteria(Vendor.class);
criteria.setFetchMode("customer", FetchMode.JOIN);
List<Vendor> listOfVendore = criteria.list();
Iterator<Vendor> veIterator = listOfVendore.iterator();
while (veIterator.hasNext()) {
Vendor vendor = (Vendor) veIterator.next();
Set<Customer> listCustomer = vendor.getChildren();
Iterator<Customer> iterator = listCustomer.iterator();
while (iterator.hasNext()) {
Customer customer = (Customer) iterator.next();
}
}
输出:
Hibernate: select this_.vendid as vendid1_1_0_, this_.vendname as vendname2_1_0_ from vendor this_
Hibernate: select children0_.vendid as vendid3_1_1_, children0_.custid as custid1_0_1_, children0_.custid as custid1_0_0_, children0_.custname as custname2_0_0_, children0_.vendid as vendid3_0_0_ from customer children0_ where children0_.vendid=?
Hibernate: select children0_.vendid as vendid3_1_1_, children0_.custid as custid1_0_1_, children0_.custid as custid1_0_0_, children0_.custname as custname2_0_0_, children0_.vendid as vendid3_0_0_ from customer children0_ where children0_.vendid=?
Hibernate: select children0_.vendid as vendid3_1_1_, children0_.custid as custid1_0_1_, children0_.custid as custid1_0_0_, children0_.custname as custname2_0_0_, children0_.vendid as vendid3_0_0_ from customer children0_ where children0_.vendid=?
Hibernate: select children0_.vendid as vendid3_1_1_, children0_.custid as custid1_0_1_, children0_.custid as custid1_0_0_, children0_.custname as custname2_0_0_, children0_.vendid as vendid3_0_0_ from customer children0_ where children0_.vendid=?
Hibernate: select children0_.vendid as vendid3_1_1_, children0_.custid as custid1_0_1_, children0_.custid as custid1_0_0_, children0_.custname as custname2_0_0_, children0_.vendid as vendid3_0_0_ from customer children0_ where children0_.vendid=?
Hibernate: select children0_.vendid as vendid3_1_1_, children0_.custid as custid1_0_1_, children0_.custid as custid1_0_0_, children0_.custname as custname2_0_0_, children0_.vendid as vendid3_0_0_ from customer children0_ where children0_.vendid=?
Hibernate: select children0_.vendid as vendid3_1_1_, children0_.custid as custid1_0_1_, children0_.custid as custid1_0_0_, children0_.custname as custname2_0_0_, children0_.vendid as vendid3_0_0_ from customer children0_ where children0_.vendid=?
映射供应商
<?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="str.Vendor" table="vendor">
<id name="vendorId" column="vendid">
<generator class="increment" />
</id>
<property name="vendorName" column="vendname" length="10" />
<set name="children" cascade="all" inverse="true">
<key column="vendid" />
<one-to-many class="str.Customer" />
</set>
</class>
</hibernate-mapping>
映射客户
<?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="str.Customer" table="customer">
<id name="customerId" column="custid">
<generator class="increment" />
</id>
<property name="customerName" column="custname"
length="10" />
<many-to-one name="parentObjets" column="vendid"
cascade="all" />
</class>
</hibernate-mapping>
答案 0 :(得分:0)
我认为Vendor
中的成员变量被称为children
,但您的查询指定FetchMode.JOIN
应该用于名为customer
的内容。确保setFetchMode
的第一个参数与Vendor
中的正确路径相对应。