views:

77

answers:

1

Hi,

I have a class hierarchy like this:

@Entity
@Table  (name="call_distribution_policies")
@Inheritance (strategy=InheritanceType.JOINED)
public class CallDistributionPolicy implements Serializable, Cloneable{
    ----------------
}

@Entity
@Table(name="skill_based_call_distribution_policies")
public class SkillBasedCallDistributionPolicy extends CallDistributionPolicy {

    --------------
}

public class CallDistributionPolicyDAOJPAImpl extends
    AbstractJPADAOImpl<CallDistributionPolicy> implements
    CallDistributionPolicyDAO {
}

    public CallDistributionPolicy get(long id) {
    try {
        Query query = entityManager
                .createQuery("from CallDistributionPolicy where id = :id");

        query.setParameter("id", id);

        List<CallDistributionPolicy> resultList = query.getResultList();

        if (!CollectionUtils.isEmpty(resultList)) {
            return resultList.get(0);
        }

        return null;
    } catch (EntityNotFoundException e) {
        return null;
    }
}
}

When I do this: log.debug(" loaded: " + callDistributionPolicyDao.get(10).toString())

It prints the toString() of the SkillsBasedCallDistributionPolicy

But when I try to cast it like this:

  SkillsBasedCallDistributionPolicy scdp =  (SkillsBasedCallDistributionPolicy) callDistributionPolicyDao.get(10)

I get class cast exception.

    com.vantage.callcenter.core.entity.acd.CallDistributionPolicy$$EnhancerByCGLIB$$334f3d1b cannot be cast to com.vantage.callcenter.core.entity.acd.SkillBasedCallDistributionPolicy 

The instanceof check fails too!

When I inspect the object in eclipse, I see the CGLIB Proxy, but as far as I understand, the CGLIB proxy should extend the SkillsBasedCallDistributionPolicy class? In the CGLIB$CALLBACK_0 property, I can see the entity class is "CallDistributionPolicy" but the target is "SkillsBasedCallDistributionPolicy".

What should be the proper process of loading the Subclass? I can see hibernate is generating all the right SQL and loading the proper subclass, but how can I check the instanceof and cast it into a subclass?

I am using hibernate 3.2.1 , Spring 2.5.5 , cglib2.1_3. Any suggestions?

+2  A: 

I know that this has been a problem in Hibernate during a long time, see for example:

And by problem, I mean bug, instanceof and casting should just work.

But I couldn't reproduce your issue with Hibernate 3.3.0.SP1. Both instanceof and casting to subclasses of a hierarchy using a joined strategy just worked. Tested with:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-cglib-repack</artifactId>
        <version>2.1_3</version>
    </dependency>

and

    <dependency>
      <groupId>javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.8.0.GA</version>
    </dependency>

I'm pretty sure there was a Jira issue for this, but couldn't find it.


The problem(bug) is consistent across my application. Can you post your working pom.xml here so I can see exactly what hibernate dependencies are you using?

Below the dependencies I used:

<project>
  ...
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.5.10</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  ...
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.4.0.GA</version>
      <exclusions>
        <exclusion>
          <groupId>javassist</groupId>
          <artifactId>javassist</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-cglib-repack</artifactId>
      <version>2.1_3</version>
    </dependency>
    ...
  </dependencies>
</project>

Here is the dependency tree:

[INFO] +- org.hibernate:hibernate-entitymanager:jar:3.4.0.GA:compile
[INFO] |  +- org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile
[INFO] |  +- org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile
[INFO] |  +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:3.3.0.SP1:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  |  \- commons-collections:commons-collections:jar:3.1:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.5.10:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  \- javax.transaction:jta:jar:1.1:compile
...
[INFO] \- org.hibernate:hibernate-cglib-repack:jar:2.1_3:compile
Pascal Thivent
Hi Pascal, Thanks for your reply. The problem(bug) is consistent across my application. Can you post your working pom.xml here so I can see exactly what hibernate dependencies are you using?
Sajid
@Sajid Sure. See my update.
Pascal Thivent
@Pascal: Thanks, I'll update my pom and see if it makes any difference.
Sajid