Hello,
My name is Christian Sprajc and I'm working for a software company which creates logistics software (
http://www.riege.com). Currently we migrate our persistence layer from ejb2 to Hibernate. In this process we discoverd a strange bug in the eager loading of collections.
We like Hibernate, because it greatly simplifies our O/R-Mapping.
We have to use Eager initalizing of Collections, because the Objects returned from hibernate are getting moved to the client over RMI.
Here is a detailed description of our problem:
Hibernate version:
Hibernate & Hibernate Annotation from CVS (Date: 5th, Dec, 2005)
Code between sessionFactory.openSession() and session.close():
Criteria c = getHibernateSession().createCriteria(TaxCode.class);
c.list();
Name and version of the database you are using:
Oracle 10i
Scenario description:
1) We have the following object model:
TaxCode <-1-------N-> TaxRates
A TaxCode can have multiple TaxRate
TaxRates are a simple eager initalized collection in TaxCode:
Excerpt from Class TaxRate:
...
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public Collection<TaxRate> getTaxRates() {
return taxRates;
}
...
Database content:
We have five (5) TaxCodes in database.
Three (3) of them have exactly one (1) TaxRate, which means the other two (2) TaxCodes have no TaxRates in their list.
The generated SQL (show_sql=true):
select this_.ASAP_OID as ASAP1_55_1_, this_.changeTime as changeTime55_1_, this_.changeUser as changeUser55_1_, this_.version as version55_1_, this_.companyOrgUnitOid as companyO5_55_1_, this_.countryOrgUnitOid as countryO6_55_1_, this_.branchOrgUnitOid as branchOr7_55_1_, this_.createOrgUnitOid as createOr8_55_1_, this_.code as code55_1_, this_.name as name55_1_, this_.validFrom as validFrom55_1_, this_.validTo as validTo55_1_, this_.presetting as presetting55_1_, this_.taxable as taxable55_1_, taxrates2_.TaxCode_ASAP_OID as TaxCode1_3_, taxrate3_.ASAP_OID as taxRates2_3_, taxrate3_.ASAP_OID as ASAP1_56_0_, taxrate3_.changeTime as changeTime56_0_, taxrate3_.changeUser as changeUser56_0_, taxrate3_.version as version56_0_, taxrate3_.rate as rate56_0_, taxrate3_.validFrom as validFrom56_0_ from TaxCode this_ left outer join TaxCode_TaxRate taxrates2_ on this_.ASAP_OID=taxrates2_.TaxCode_ASAP_OID left outer join TaxRate taxrate3_ on taxrates2_.taxRates_ASAP_OID=taxrate3_.ASAP_OID
Problem description:
The problem occours when we switch the Collection of TaxRate (getTaxRates) to eager loading.
When executing a findAll on the TaxCodes table (see "Code between sessionFactory.openSession() and session.close()")
the returned collection contains eight (8) elements! Three (3) of them are actually duplicate entries of the same TaxCode.
The correct result should be five (5) TaxCodes in the collection.
The strange thing happends when changing the getTaxRates() collection on TaxCode to Lazy loading.
The returned list returned by the Critiera object (see "Code between sessionFactory.openSession() and session.close()") contains the expected five (5) results of TaxCode.
We are looking forward to hear from anyone who can help.
Best regards,
Christian Sprajc