We're updating the issue view to help you get more done. 

session.load returns sometimes wrong class when using polymorphism, session.get is ok

Description

Hi

First I'll try to briefly describe parts of my aplication that are relevant so the problem would be understood.

I have an applicaition that can list, modify, edit and delete "Business Cases" and its "Widgets"; CRUD. I use Spring framework and it's DAO support. Application layers are: presentation (JSF + Beans), services, DAO. I use Spring's transaction management and "open session in view" pattern.

There are two Business Case types: Single and Table.

Now, I am on the Business Case list page. There is listed one Single Business Case already so I perform an action to list all its Widgets. This action follows to the view with list of the Widgets. I get the list by calling in presentation layer (JSF managed bean):

private BusinessCase businessCase;

public String prepareList(Integer businessCaseId) {
// will print com.kovine.kfe.model.BusinessCaseSingleImpl
System.out.println(businessCaseService.getBusinessCase(businessCaseId).getClass().getName());

// will print com.kovine.kfe.model.BusinessCaseSingleImpl@3e25d33
System.out.println(businessCaseService.getBusinessCase(businessCaseId).toString());

businessCase = businessCaseService.getBusinessCase(businessCaseId);
businessCase.getWidgets();
...
}

Now, I invoke an action to generate all the widgets (new HTTP request). The source is in the same file as that from previous one.

public String actionAddAllWidgets() {
Integer businessCaseId = businessCase.getId();

// will print com.kovine.kfe.model.BusinessCaseTable$$EnhancerByCGLIB$$b36baad4 <----- ??? why BusinessCaseTable ???
// sometimes will print com.kovine.kfe.model.BusinessCase$$EnhancerByCGLIB$$b36baad4 <----- ??? why not BusinessCaseSingle ???
System.out.println(businessCaseService.getBusinessCase(businessCaseId).getClass().getName());

// will print com.kovine.kfe.model.BusinessCaseSingleImpl@19f167a
// now in the log i see the database was queried (SELECT) right during .toString() method
System.out.println(businessCaseService.getBusinessCase(businessCaseId).toString());

// now i want to refresh businessCase to get no LazyInit exception
businessCase = businessCaseService.getBusinessCase(businessCaseId);
...
}

If I use get() instead of load(), no proxy is used and the classes are OK (
com.kovine.kfe.model.BusinessCaseSingleImpl
com.kovine.kfe.model.BusinessCaseSingleImpl@e5ab411
)

Some more relevant code:
------------------------------------ SERVICE:
public BusinessCase getBusinessCase(Serializable businessCaseId) {
return dao.getBusinessCase(businessCaseId);
}

DAO:
public BusinessCase getBusinessCase(Serializable businessCaseId) {
// get() causes no problem, but now I am talking about bug in load()
// return (BusinessCase) this.getHibernateTemplate().get(BusinessCaseImpl.class, businessCaseId);

return (BusinessCase) this.getHibernateTemplate().load(BusinessCaseImpl.class, businessCaseId);
}

MODEL CLASSES:
public interface BusinessCase extends Identifiable
public abstract class BusinessCaseImpl implements BusinessCase
public interface BusinessCaseSingle
public class BusinessCaseSingleImpl extends BusinessCaseImpl implements BusinessCaseSingle
public interface BusinessCaseTable
public class BusinessCaseTableImpl extends BusinessCaseImpl implements BusinessCaseTable

HIBERNATE MAPPING:
<hibernate-mapping package="com.kovine.kfe.model">
<class name="BusinessCaseImpl" table="KFE_BUSINESS_CASE" abstract="true" proxy="BusinessCase">
...

<joined-subclass table="KFE_BUSINESS_CASE_SINGLE" name="BusinessCaseSingleImpl" proxy="BusinessCaseSingle">
<key column="BUSINESS_CASE_ID" />
...
</joined-subclass>

<joined-subclass table="KFE_BUSINESS_CASE_TABLE" name="BusinessCaseTableImpl" proxy="BusinessCaseTable">
<key column="BUSINESS_CASE_ID" />
...
</joined-subclass>
</class>
</hibernate-mapping>

I hope you will understand my description. If anything comes unclear, please let me know (comment). I can't prepare testcase for you because it would require my expensive effort and for now I can workaround this bug using get() instead of load().

Thanks in advance.

Environment

java 1.4.x, tomcat 5.0.x, spring 1.2.6, MyFaces 1.1.1, CGLIB 2.1_3

Status

Assignee

Unassigned

Reporter

Martin Zdila

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

3.1

Priority

Major