Hibernate left join fetch拿到多的一方值问题

问题域:

     bill货物单 ----->{1:n关系} ware货物

Bill.java
...
Set<Ware> wareSet = new HashSet<Ware>();
....


Ware.java
...
Bill bill;
....

要使得从Bill端拿到wareset
则注意:
1. hql: "from Bill bill left join fetch bill.wareSet ware"
2. 两边建立双向关系
   bill.hbm.xml
<set name="wareSet" inverse="true">
<key column="billwareid" />
<one-to-many class="Ware"/>
</set>


ware.hbm.xml

        <many-to-one name="bill"                   
           column="billwareid"
           unique="true" />

3.注意点

   public void addBill(Bill bill) {
        billDao.insert(bill);
        Iterator<Ware> iter = bill.getWareSet().iterator();
        while(iter.hasNext()){  
        Ware ware = iter.next();
        ware.setBill(bill);//如果这句将上面的hql不会拿到wareset中的ware
        wareDao.insert(ware);        
        }
}


你可能感兴趣的:(java,xml,Hibernate)