java - 尝试将实例转换为 PersistenceCapable 失败。确保它已得到增强

标签 java jpa jpa-2.0 openjpa

我是第一次试用 OpenJPA 2.0.1,并且得到:

79  WARN   [main] openjpa.Runtime - The configuration property named "openjpa.Id" was not recognized and will be ignored, although the name closely matches a valid property called "openjpa.Id".
179  INFO   [main] openjpa.Runtime - Starting OpenJPA 2.0.1
371  INFO   [main] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.PostgresDictionary" (PostgreSQL 8.4.4 ,PostgreSQL Native Driver PostgreSQL 9.0 JDBC4 (build 801)).
<openjpa-2.0.1-r422266:989424 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Attempt to cast instance "x.quality.QualityQuery@4c4b11e9" to PersistenceCapable failed.  Ensure that it has been enhanced.
FailedObject: x.quality.QualityQuery@4c4b11e9
    at org.apache.openjpa.kernel.BrokerImpl.assertPersistenceCapable(BrokerImpl.java:4559)
    at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2561)
    at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2423)
    at org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1069)
    at org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:706)
    at ...

根据 the OpenJPA docs ,我不需要做任何事情来在运行时获得基本的增强:

2.5. Omitting the OpenJPA enhancer

OpenJPA does not require that the enhancer be run. If you do not run the enhancer, OpenJPA will fall back to one of several possible alternatives for state tracking, depending on the execution environment.

Java 6 class retransformation:

if you are running your application in a Java 6 environment, OpenJPA will attempt to dynamically register a ClassTransformer that will redefine your persistent classes on the fly to track access to persistent data. Additionally, OpenJPA will create a subclass for each of your persistent classes. When you execute a query or traverse a relation, OpenJPA will return an instance of the subclass. This means that the instanceof operator will work as expected, but o.getClass() will return the subclass instead of the class that you wrote. You do not need to do anything at all to get this behavior. OpenJPA will automatically detect whether or not the execution environment is capable of Java 6 class retransformation.

为什么这不起作用的任何线索?非常感谢...

(为什么会出现 openjpa.Id 警告的加分 - 我没有设置任何此类属性...)

最佳答案

啊哈,原来答案是传入:

"openjpa.RuntimeUnenhancedClasses" => RuntimeUnenhancedClassesModes.SUPPORTED

到配置图。为什么没有更清楚地记录这一点,我不确定...

关于java - 尝试将实例转换为 PersistenceCapable 失败。确保它已得到增强,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4102294/

相关文章:

java - Java中的字符串池是什么?

java - 从导入建议中删除 AWT 工件

java - 如何解决回文中的 java.lang.NumberFormatException?

java - 为什么我在 ManyToOne 关联中看不到 Hibernate 代理对象?

jpa - 更新 JPA 实体中的一个字段

java - JPA2 CreateQuery SQL Server 方言

java - 在 Tomcat 上安装游戏。新人在这里

java - JPA 3 向连接注解

java - 对 CompositeKey 使用 JoinColumn 两次获取重复列异常

java - 如何在jpql中为in-clause设置集合项?