Attempt to cast instance to PersistenceCapable failed. Ensure that it's been enhanced

10,250

Aha, turns out that the answer was to pass in:

"openjpa.RuntimeUnenhancedClasses" => RuntimeUnenhancedClassesModes.SUPPORTED

to the configuration map. Why this is not documented more clearly, I'm not sure...

Share:
10,250
Steven Schlansker
Author by

Steven Schlansker

Updated on June 05, 2022

Comments

  • Steven Schlansker
    Steven Schlansker almost 2 years

    I'm trying out OpenJPA 2.0.1 for the first time, and am getting:

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

    According to the OpenJPA docs, I shouldn't have to do anything to get the basic enhancement at runtime:

    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.

    Any clues why this isn't working? Thanks much...

    (Bonus points for why the openjpa.Id warning happens - I haven't set any such property...)

  • Rick
    Rick over 13 years
    I highly recommend NOT using that property. Please read openjpa.apache.org/entity-enhancement.html for more info.
  • Steven Schlansker
    Steven Schlansker over 13 years
    @Rick: right now I'm just playing around and I really don't want to have to add a build step between every compile / run cycle...
  • Rick
    Rick over 13 years
    I'd suggest using the -javaagent.... If you're running in eclipse it's super easy to get going.
  • Steven Schlansker
    Steven Schlansker over 13 years
    I tried that but it didn't seem to work. Didn't print out any messages at all (I have DEBUG level enabled), and it complained that the classes were not enhanced.