openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Wolf <>
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

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
	public Set<RiskFactor> getRiskFactors() {
		return this.riskFactors;

Second M2M entity "RiskFactor":

	//bi-directional many-to-many association to MarketData
		, 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 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
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:

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:

Next, I tried invoking with:


Same error - same stack trace.

Then, I tried setting this property:

Same error - same stack trace.

Then, following a suggestion I found here:

I tried setting both:

BTW, this is a dead link
"User's Guide on Enhancement" /
(from page:

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.



View raw message