openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Wolf <cwolf.a...@gmail.com>
Subject Re: cannot persist m2m data, getting "ArgumentException: Attempt to cast instance xxx [java.util.HashSet]" to PersistenceCapable failed. Ensure that it has been enhanced."
Date Mon, 19 Nov 2012 15:01:15 GMT
Herman,

That was a great tip! Thanks - since I was running "mvn
openjpa:enhance" from the command line yet running
from Eclipse, and the project had "Build Automatically" enabled -
Eclipse was re-compiling on top
of the enhanced classes, so I turned that off and only compile/enhance
from Maven such that I am definitely
always running with enhanced entities.

The issue still remains that since I have M-M via Set (or List), when
I run it complains about
java.util.LinkedHashSet, or if trying with lists, it complains about
java.utl.ArrayList.

The error remains:

182  openjpa  INFO   [main] openjpa.Runtime - OpenJPA dynamically
loaded the class enhancer. Any classes that were not enhanced at build
time will be enhanced when they are loaded by the JVM.
205  openjpa  INFO   [main] openjpa.Runtime - Starting OpenJPA 2.2.0
236  openjpa  INFO   [main] openjpa.jdbc.JDBC - Using
dictionary class "org.apache.openjpa.jdbc.sql.OracleDictionary".
Exception in thread "main" <openjpa-2.2.0-r422266:1244990 nonfatal
user error> org.apache.openjpa.persistence.ArgumentException:
Attempt to cast instance
"[persistdemo.ojpa.entities.RiskFactor@7857ae58]
[java.util.ArrayList]"
to PersistenceCapable failed.  Ensure that it has been enhanced.

So it seems to be complaining about ArrayList - not the entity, RiskFactor.

So then I tried putting this in orm.xml:

  <persistence-unit-metadata>
      <embeddable class="java.util.ArrayList" metadata-complete="true"/>
  </persistence-unit-metadata>

...and sure enough, that seems to ask OpenJPA to use non-enhanced, but
sub-classed version
of ArrayList:

766  openjpa  INFO   [main] openjpa.Enhance - Creating subclass and
redefining methods for
"[class java.util.ArrayList]". This means that your application will
be less efficient than it would if you ran the OpenJPA enhancer.

..but right after that log message, I see this:

Exception in thread "main" <openjpa-2.2.0-r422266:1244990 fatal user
error> org.apache.openjpa.persistence.ArgumentException: No registered
metadata for type "class java.util.ArrayList".
This can happen if this class has not been annotated as a persistent
 entity or specified in the persistence unit (ex: in the orm.xml).

Very confusing since I *do* specify it in orm.xml.

Thanks,

   -Chris

On Mon, Nov 19, 2012 at 3:32 AM, hvieren <herman.vierendeels@gmail.com> wrote:
> does
> javap -classpath your_classpath  persistdemo/ojpa/entities/RiskFactor
>
> reveal that it implements
>  implements java.io.Serializable,org.apache.openjpa.enhance.PersistenceCapable
>
> Herman
>
>
> 2012/11/19 maxtorzito [via OpenJPA] <ml-node+s208410n7581817h75@n2.nabble.com>:
>> Of course I'm using 1-M relationships but I use them with List not hashset,
>> why you dont give a try to list only for look if the problem is with
>> hashset.
>> El 18/11/2012 19:59, "Chris Wolf" <[hidden email]> escribió:
>>
>>> José,
>>>
>>> Thanks for your reply.  My pom.xml setup is very similar to yours and
>>> the "enhance" goal  runs without error, but, as I mentioned,
>>> I still get the error on "casting java.util.HashSet".   Are you able
>>> to persist entities with 1-M or M-M relationships?
>>>
>>> BTW, I changed my code to temporarily change JPA provider from OpenJPA
>>> to the Hibernate JPA provider
>>> and, once again, Hibernate's JPA reports:
>>>
>>> Exception in thread "main" java.lang.IllegalArgumentException: Unknown
>>> entity: java.util.HashSet
>>>         at
>>>
>>> org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:671)
>>>
>>> Well, at least both JPA implementations are complaining about the same
>>> issue - I just wish I knew
>>> what I was doing!   I know I can get this to work in an instant by
>>> reverting to Hibernate-only
>>> solution, but I was hoping the byte-code weaving of JPA would be a
>>> performance advantage over
>>> Hibernate's reflection/proxy pattern.
>>>
>>> Thanks again,
>>>
>>>     -Chris
>>>
>>> On Sun, Nov 18, 2012 at 1:26 PM, José Luis Cetina <[hidden email]>
>>> wrote:
>>> > I have this in my pom.xml for ENHANCMENT and works:
>>> >
>>> > <!--OPENJPA ENHANCMENT-->
>>> >             <plugin>
>>> >                 <groupId>org.apache.openjpa</groupId>
>>> >                 <artifactId>openjpa-maven-plugin</artifactId>
>>> >                 <version>2.2.0</version>
>>> >                 <configuration>
>>> >                     <includes>mypackage/model/*.class</includes>
>>> >                     <addDefaultConstructor>true</addDefaultConstructor>
>>> >
>>> > <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
>>> >
>>> > <connectionDriverName>com.mysql.jdbc.Driver</connectionDriverName>
>>> >                     <connectionProperties>
>>> >                         driverClass=${database.driver.name},
>>> >                         jdbcUrl=${database.connection.url},
>>> >                         user=${database.user},
>>> >                         password=${database.password},
>>> >                         minPoolSize=5,
>>> >                         acquireRetryAttempts=3,
>>> >                         maxPoolSize=20
>>> >                     </connectionProperties>
>>> >                 </configuration>
>>> >                 <executions>
>>> >                     <execution>
>>> >                         <id>enhancer</id>
>>> >                         <phase>process-classes</phase>
>>> >                         <goals>
>>> >                             <goal>enhance</goal>
>>> >                         </goals>
>>> >                     </execution>
>>> >                 </executions>
>>> >                 <dependencies>
>>> >                     <dependency>
>>> >                         <groupId>org.apache.openjpa</groupId>
>>> >                         <artifactId>openjpa</artifactId>
>>> >                         <version>2.2.0</version>
>>> >                     </dependency>
>>> >                 </dependencies>
>>> >             </plugin>
>>> >
>>> > In my persistence.xml i have:
>>> >
>>> >      <property name="openjpa.ConnectionUserName"
>>> > value="${db.username}"/>
>>> >       <property name="openjpa.ConnectionPassword"
>>> value="${db.password}"/>
>>> >       <property name="openjpa.ConnectionURL" value="${db.url}"/>
>>> >       <property name="openjpa.ConnectionDriverName"
>>> > value="${db.driver.class}"/>
>>> >
>>> > You can replace the database properties in persistence.xml with your own
>>> > values (${db.username},${db.password},${db.url},${db.driver.class}).
>>> >
>>> >
>>> > I use this configuration for my JEE Projects.
>>> >
>>> > Maybe this can help you.
>>> >
>>> > Regards.
>>> >
>>> > SCJA. JL Cetina
>>> >
>>> >
>>> > 2012/11/18 Chris Wolf <[hidden email]>
>>> >
>>> >> I wrote a shell script to directly invoke PCEnhancer on class
>>> >> java.util.HashSet,
>>> >> and even that didn't work:
>>> >>
>>> >> $ ./enhance.sh
>>> >> 52  openjpa  INFO   [main] openjpa.Tool - Enhancer running on type
>>> >> "java.util.HashSet".
>>> >> Exception in thread "main" java.lang.RuntimeException:
>>> >> java.io.FileNotFoundExcep
>>> >> tion: file:\C:\opt\jdk\jre\lib\rt.jar!\java\util\HashSet.class (The
>>> >> filename, directory name, or volume label syntax is incorrect)
>>> >>         at
>>> >>
>>>
>>> org.apache.openjpa.lib.conf.Configurations.launchRunnable(Configurations.java:744)
>>> >>
>>> >> On Sun, Nov 18, 2012 at 10:37 AM, Chris Wolf <[hidden email]>
>>> wrote:
>>> >> > Hello,
>>> >> >
>>> >> > This is my first posting and first attempt to use OpenJPA.  I put
>>> >> > together a quick demo and can persist
>>> >> > individual, unrelated entities.  However, when I try to persist
>>> >> > related entities to two tables via a link table, i.e. many-2-many,
>>> >> > it keeps complaining about "casting to PersistenceCapable", in
>>> >> > particular the class "java.util.HashSet".
>>> >> >
>>> >> > First, I am using the Eclipse JPA plugin (called "Dali" or
>>> >> > "EclipseLink").  Of course, I have OpenJPA configured
>>> >> > as my JPA provider, I am in a plain Java SE environment with
>>> >> > LOCAL_RESOURCE via JDBC connection
>>> >> > properties in the persistence.xml.  I am using Sun/Oracle 64bit
>>> >> > JDK-1.6 and OpenJPA-2.2.0.
>>> >> >
>>> >> > I am using the Eclipse JPA plugin to generate the entity classes
from
>>> >> > already-exiting database schema objects,
>>> >> > and that code looks like (just pasting the relationship code),
this
>>> >> > action also adds these classes to persistence.xml
>>> >> > via persistence-unit/class elements.
>>> >> >
>>> >> > First M2M entity, "MarketData":
>>> >> >
>>> >> >         //bi-directional many-to-many association to RiskFactor
>>> >> >         @ManyToMany(mappedBy="marketData")
>>> >> >         public Set<RiskFactor> getRiskFactors() {
>>> >> >                 return this.riskFactors;
>>> >> >         }
>>> >> >
>>> >> > Second M2M entity "RiskFactor":
>>> >> >
>>> >> >         //bi-directional many-to-many association to MarketData
>>> >> >     @ManyToMany
>>> >> >         @JoinTable(
>>> >> >                 name="MARKET_DATA__RISK_FACTOR"
>>> >> >                 , joinColumns={
>>> >> >                         @JoinColumn(name="RISK_FACTOR_ID",
>>> >> nullable=false)
>>> >> >                         }
>>> >> >                 , inverseJoinColumns={
>>> >> >                         @JoinColumn(name="MARKET_DATA_ID",
>>> >> nullable=false)
>>> >> >                         }
>>> >> >                 )
>>> >> >         public Set<MarketData> getMarketData() {
>>> >> >                 return this.marketData;
>>> >> >         }
>>> >> >
>>> >> > When I run the code, the log indicates implicit runtime enhacement,
>>> >> > yet it is complaining:
>>> >> >
>>> >> > "[persistdemo.ojpa.entities.RiskFactor@61578aab] [java.util.HashSet]"
>>> >> > to PersistenceCapable failed.  Ensure that it has been enhanced."
>>> >> >
>>> >> > ...when it says, "Ensure that it has been enhanced." - which is
"it"
>>> >> > referring to?  The entity "RiskFactor" or the field relationship
>>> >> > field's class, "java.util.HashSet"?
>>> >> >
>>> >> >
>>> >> > 186  openjpa  INFO   [main] openjpa.Runtime - OpenJPA dynamically
>>> >> > loaded the class enhancer. Any classes that were not enhanced at
>>> >> > build
>>> >> > time will be enhanced when they are loaded by the JVM.
>>> >> > SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
>>> >> > SLF4J: Defaulting to no-operation (NOP) logger implementation
>>> >> > SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for
>>> >> > further details.
>>> >> > 243  openjpa  INFO   [main] openjpa.Runtime - OpenJPA dynamically
>>> >> > loaded a validation provider.
>>> >> > 596  openjpa  INFO   [main] openjpa.Runtime - Starting OpenJPA
2.2.0
>>> >> > 630  openjpa  INFO   [main] openjpa.jdbc.JDBC - Using dictionary
>>> >> > class
>>> >> > "org.apache.openjpa.jdbc.sql.OracleDictionary".
>>> >> > Exception in thread "main" <openjpa-2.2.0-r422266:1244990 nonfatal
>>> >> > user error> org.apache.openjpa.persistence.ArgumentException:
Attempt
>>> >> > to cast instance "[persistdemo.ojpa.entities.RiskFactor@61578aab]
>>> >> > [java.util.HashSet]" to PersistenceCapable failed.  Ensure that
it
>>> >> > has
>>> >> > been enhanced.
>>> >> >
>>> >> > Maybe because "java.util.HashSet" was loaded before the dynamic
>>> >> > enhancer could get to it?
>>> >> >
>>> >> > Next, I tried performing build-time enhancement via Maven, per
this
>>> doc:
>>> >> > http://openjpa.apache.org/enhancement-with-maven.html
>>> >> >
>>> >> > When I ran "mvn openjpa:enhance", it finished with success, but
none
>>> >> > of the classes in target/classes seemed to have be changed
>>> >> > (last-modified date same as compile-time).  and re-running results
in
>>> >> > the same error and stack-trace.
>>> >> >
>>> >> > My openjpa:enhance configuration was:
>>> >> > <configuration>
>>> >> >     <includes>**/entities/*.class,java.util.HashSet</includes>
>>> >> >      <addDefaultConstructor>true</addDefaultConstructor>
>>> >> >      <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
>>> >> > </configuration>
>>> >> >
>>> >> >
>>> >> > Next, I tried invoking with:
>>> >> >
>>> >> > -javaagent:/opt/apache-openjpa-2.2.0/openjpa-all-2.2.0.jar
>>> >> >
>>> >> > Same error - same stack trace.
>>> >> >
>>> >> > Then, I tried setting this property:
>>> >> > openjpa.RuntimeUnenhancedClasses=supported
>>> >> >
>>> >> > Same error - same stack trace.
>>> >> >
>>> >> >
>>> >> > Then, following a suggestion I found here:
>>> >> >
>>> >>
>>>
>>> http://openjpa.208410.n2.nabble.com/JPA-adding-entities-to-EntityManagerFactory-programmatically-td210697.html
>>> >> >
>>> >> > I tried setting both:
>>> >> > openjpa.RuntimeUnenhancedClasses=supported
>>> >> > openjpa.MetaDataFactory=jpa(Types=java.util.HashSet)
>>> >> >
>>> >> > BTW, this is a dead link
>>> >> > "User's Guide on Enhancement" /
>>> >> >
>>> >>
>>>
>>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_pc_enhance
>>> >> > (from page: http://openjpa.apache.org/entity-enhancement.html)
>>> >> >
>>> >> > So is there any way to use OpenJPA to persist objects related via
a
>>> >> > link table?  (there obviously must be,
>>> >> > but it's a total mystery to me)  I can't believe it's this difficult,
>>> >> > I must be doing something really dumb.
>>> >> >
>>> >> > Regards,
>>> >> >
>>> >> > CW
>>> >>
>>> >
>>> >
>>> >
>>> > --
>>> > -------------------------------------------------------------------
>>> > *SCJA. José Luis Cetina*
>>> > -------------------------------------------------------------------
>>>
>>
>>
>> ________________________________
>> If you reply to this email, your message will be added to the discussion
>> below:
>> http://openjpa.208410.n2.nabble.com/cannot-persist-m2m-data-getting-ArgumentException-Attempt-to-cast-instance-xxx-java-util-HashSet-to--tp7581813p7581817.html
>> To start a new topic under OpenJPA Users, email
>> ml-node+s208410n208411h89@n2.nabble.com
>> To unsubscribe from OpenJPA Users, click here.
>> NAML
>
>
>
>
> --
> View this message in context: http://openjpa.208410.n2.nabble.com/cannot-persist-m2m-data-getting-ArgumentException-Attempt-to-cast-instance-xxx-java-util-HashSet-to--tp7581813p7581818.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message