JPA继承entitymanager.find产生ClassCastException [英] JPA Inheritance entitymanager.find produces 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>实现
CallDistributionPolicyDAO {
}
public CallDistributionPolicy get(long id){
try {
Query query = entityManager
.createQuery( from CallDistributionPolicy where id =:id);
query.setParameter(id,id);
列表< CallDistributionPolicy> resultList = query.getResultList();
if(!CollectionUtils.isEmpty(resultList)){
return resultList.get(0);
}
返回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不能转换为com.vantage.callcenter.core.entity。 acd.SkillBasedCallDistributionPolicy
instanceof check也会失败!
当我在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延迟加载
- JPA:instanceof vs. hibernate proxy:何时安全? .blogspot.com / 2009/05 / how-to-avoid-classcastexceptions-when.htmlrel =nofollow noreferrer>如何在使用Hibernate Proxied Objects时避免ClassCastExceptions )
问题所在,我的意思是错误, instanceof
,并且投射应该是正常的。
但我无法用Hibernate 3.3.0.SP1重现您的问题。 instanceof
和使用联合策略转换为层次结构的子类都行得通。经测试:
<依赖关系>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-cglib-repack< / artifactId>
< version> 2.1_3< / version>
< /依赖关系>
和
< dependency>
< groupId> javassist< / groupId>
< artifactId> javassist< / artifactId>
< version> 3.8.0.GA< / version>
< /依赖关系>
我很确定这有一个Jira问题,但找不到它。 / p>
问题(错误)在我的应用程序中是一致的。你可以在这里发布你的工作pom.xml,所以我可以确切地看到你正在使用的hibernate依赖关系吗?
< project>
...
< dependencyManagement>
<依赖关系>
< dependency>
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-api< / artifactId>
< version> 1.5.10< / version>
< /依赖关系>
< /依赖关系>
< / dependencyManagement>
...
< dependencies>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> 3.4.0.GA< / version>
<排除项>
<排除>
< groupId> javassist< / groupId>
< artifactId> javassist< / artifactId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-cglib-repack< / artifactId>
< version> 2.1_3< / version>
< /依赖关系>
...
< /依赖关系>
< / project>
以下是依赖关系树:
[INFO] | + - org.hibernate:ejb3-persistence:jar:1.0.2.GA:编译
[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:编译
[INFO] | | \- commons-collections:commons-collections:jar:3.1:编译
[INFO] | + - org.slf4j:slf4j-api:jar:1.5.10:compile
[INFO] | + - dom4j:dom4j:jar:1.6.1:编译
[INFO] | | \ -xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] | \- javax.transaction:jta:jar:1.1:编译
...
[INFO] \- org.hibernate:hibernate-cglib-repack:jar:2.1_3:compile
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?
I know that this has been a problem in Hibernate during a long time, see for example:
- ClassCastException cglib lazy loading
- JPA: instanceof vs. hibernate proxy: when is it safe?
- How to Avoid ClassCastExceptions when using Hibernate Proxied Objects)
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
这篇关于JPA继承entitymanager.find产生ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!