hibernate fetchmode如何加入工作?

时间:2018-01-25 10:13:30

标签: hibernate

我与VendorCustomer的关系。当我使用条件执行查询并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>

1 个答案:

答案 0 :(得分:0)

我认为Vendor中的成员变量被称为children,但您的查询指定FetchMode.JOIN应该用于名为customer的内容。确保setFetchMode的第一个参数与Vendor中的正确路径相对应。