We have an instance of Vendor class, that has a collection of Accounts that are lazily loaded. If we get the vendor, then run a query for all Vendors and then access the accounts of the original vendor, Hibernate appends multiple vendor IDs in the where clause, because we ran the all vendors query in between.
The strange thing is it always appends 10 vendor ids whereas we have around 25 vendors in the table. Not sure if it appends same vendor id 10 times or they are 10 different vendors.
We do not want to use session.clear(), since we are in the middle of a transaction. Below are the details.
Code:
Hibernate version: 2.1.7
Mapping documents:
Code:
<class name=".Vendor" dynamic-update="true" optimistic-lock="none" table="TELCO" lazy="true">
<id name="id">
<column name="TELCO_ID"/>
<generator class="HibernateSequenceGenerator"/>
</id>
<dynamic-component name="values">
<set name="accounts" lazy="true" batch-size="100" cascade="all-delete-orphan" inverse="true">
<key column="BILLING_TELCO_ID"/>
<one-to-many class="BillingAccount"/>
</set>
</dynamic-component>
</class>
<class name="BillingAccount" dynamic-update="true" optimistic-lock="none" table="BILLING" lazy="true">
<id name="id">
<column name="B_ID"/>
<generator class="HibernateSequenceGenerator"/>
</id>
<many-to-one name="vendor" class="Vendor" column="BILLING_TELCO_ID"/>
</dynamic-component>
</class>
Test Program:
Code:
public void testBug()
{
//Get single vendor by ID
Vendor myVendor = Vendor.findByKey(new Long(193));
//Get all vendors
ObjectList<Vendor> vendors = Vendor.selectAllVendors().execute();
for (Vendor vendor : vendors)
{
vendor.getLongTelcoName();
}
//Now get all accounts for the single vendor, query appends OR clause
Iterator accountsIterator = myVendor.getAccounts().iterator();
while (accountsIterator.hasNext())
{
BillingAccount account = (BillingAccount) accountsIterator.next();
}
}
Query is incorrect:
Code:
<select accounts0_.BILLING_TELCO_ID as BILLING17___, ...
from BILLING accounts0_ where ((accounts0_.BILLING_TELCO_ID=?) or (accounts0_.BILLING_TELCO_ID=?) or (accounts0_.BILLING_TELCO_ID=?) or (accounts0_.BILLING_TELCO_ID=?) or (accounts0_.BILLING_TELCO_ID=?) or (accounts0_.BILLING_TELCO_ID=?) or (accounts0_.BILLING_TELCO_ID=?) or (accounts0_.BILLING_TELCO_ID=?) or (accounts0_.BILLING_TELCO_ID=?) or (accounts0_.BILLING_TELCO_ID=?))>
Thanks in advance,
PL