java – JPA Inheritance entitymanager.find生成ClassCastException

我有这样的类层次结构:

@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;
    }
}
}

当我这样做:
     log.debug(“loaded:”callDistributionPolicyDao.get(10).toString())

它打印SkillsBasedCallDistributionPolicy的toString()

但是当我尝试像这样投射时:

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

我得到了类强制转换异常.

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

检查实例也失败了!

当我在eclipse中检查对象时,我看到了CGLIB代理,但据我所知,CGLIB代理应该扩展SkillsBasedCallDistributionPolicy类?在CGLIB $CALLBACK_0属性中,我可以看到实体类是“CallDistributionPolicy”,但目标是“SkillsBasedCallDistributionPolicy”.

加载子类的正确过程应该是什么?我可以看到hibernate正在生成所有正确的SQL并加载正确的子类,但是我如何检查instanceof并将其转换为子类?

我正在使用hibernate 3.2.1,Spring 2.5.5,cglib2.1_3.有什么建议?

最佳答案 我知道这在Hibernate中长期存在问题,例如:

> ClassCastException cglib lazy loading
> JPA: instanceof vs. hibernate proxy: when is it safe?
> How to Avoid ClassCastExceptions when using Hibernate Proxied Objects)

问题是,我的意思是bug,instanceof和cast应该正常工作.

但是我无法用Hibernate 3.3.0.SP1重现你的问题.使用连接策略的实例和转换为层次结构的子类都正常工作.经测试:

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

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

我很确定这有一个Jira问题,但找不到它.

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?

在我使用的依赖项下面:

<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>

这是依赖树:

[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
点赞