openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Wolf <cwolf.a...@gmail.com>
Subject cannot persist m2m data, getting "ArgumentException: Attempt to cast instance xxx [java.util.HashSet]" to PersistenceCapable failed. Ensure that it has been enhanced."
Date Sun, 18 Nov 2012 15:37:15 GMT
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

Mime
View raw message