I choose property access over field access due to the caveats listed at the bottom of this page:
http://openjpa.apache.org/builds/1.0.4/apache-openjpa-1.0.4/docs/manual/ref_guide_pc_enhance.html ...not that is matters, really, since my entities *are* enhanced (just not java.util.ArrayList). I would be curious to see your persistence.xml and orm.xml and if you are doing runtime enhancement or build-time enhancement.... Thanks, -Chris On Mon, Nov 19, 2012 at 10:37 AM, Boblitz John <john.bobl...@bertschi.com> wrote: > Actually, mine works just fine! > > I choose to annotate the attributes instead of the methodes, but here is an > example: > > @Entity > @Table(name = "UserGroup") > public class UserGroup extends BaseEntity { > > [snip] > > @ManyToMany(mappedBy = "userGroups") > private Set<Users> users; > > } > > @Entity > @Table(name = "Users") > public class Users extends BaseEntity { > > [snip] > > @ManyToMany > @JoinTable(name = "UserGroupMember", joinColumns = @JoinColumn(name = > "userid"), inverseJoinColumns = @JoinColumn(name = "groupid")) > private Set<UserGroup> userGroups; > > } > > > > >> -----Ursprüngliche Nachricht----- >> Von: Chris Wolf [mailto:cwolf.a...@gmail.com] >> Gesendet: Montag, 19. November 2012 16:29 >> An: users@openjpa.apache.org >> Betreff: Re: cannot persist m2m data, getting >> "ArgumentException: Attempt to cast instance xxx >> [java.util.HashSet]" to PersistenceCapable failed. Ensure >> that it has been enhanced." >> >> José, >> >> The plugin doc says you can have a comma-delimited list of >> classes, however, since enhancement on collection classes >> doesn't seem to work anyway - per your suggestion, I removed >> "java.util.ArrayList" and re-ran the "mvn openjpa:enhance" command. >> >> The result, as ever, is still: >> >> 771 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. >> 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). >> >> I wonder if anyone knows of a working example of many-to-many >> ORM using OpenJPA? >> >> Thanks, >> >> -Chris >> >> >> On Mon, Nov 19, 2012 at 10:19 AM, José Luis Cetina >> <maxtorz...@gmail.com> wrote: >> > And I think you don't have to set arraylist class or any >> java "native" >> > class as enhance class. Only your entities have to be enhancement. >> > El 19/11/2012 09:12, "José Luis Cetina" >> <maxtorz...@gmail.com> escribió: >> > >> >> I'm not sure if the include tag accept more than 1 class. >> >> >> >> You have: >> >> <includes>**/entities/*.class,java.util.ArrayList.class</includes> >> >> >> >> Try to remove java.util.ArrayList. >> >> El 19/11/2012 09:05, "Chris Wolf" <cwolf.a...@gmail.com> escribió: >> >> >> >>> José, >> >>> >> >>> Thanks - I sent the two entities to John (this list). >> The pom.xml >> >>> is attached... >> >>> >> >>> -Chris >> >>> >> >>> >> >>> >> >>> On Mon, Nov 19, 2012 at 9:54 AM, José Luis Cetina >> >>> <maxtorz...@gmail.com> >> >>> wrote: >> >>> > Please send your 2 entities and your pom configuration whit the >> >>> > enhance plugin. >> >>> > El 19/11/2012 08:46, "Chris Wolf" >> <cwolf.a...@gmail.com> escribió: >> >>> > >> >>> >> José, >> >>> >> >> >>> >> I tried List/ArrayList - same error. Thanks.... >> >>> >> >> >>> >> -Chris >> >>> >> >> >>> >> On Sun, Nov 18, 2012 at 9:47 PM, José Luis Cetina < >> >>> maxtorz...@gmail.com> >> >>> >> wrote: >> >>> >> > 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" >> <cwolf.a...@gmail.com> 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(AbstractEntityMa >> >>> nagerImpl.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 < >> >>> maxtorz...@gmail.com >> >>> >> > >> >>> >> >> 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</enforcePropertyRestrictio >> >>> >> >> > ns> >> >>> >> >> > >> >>> >> >> > >> <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 <cwolf.a...@gmail.com> >> >>> >> >> > >> >>> >> >> >> 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(Configurat >> >>> ions.java:744) >> >>> >> >> >> >> >>> >> >> >> On Sun, Nov 18, 2012 at 10:37 AM, Chris Wolf < >> >>> cwolf.a...@gmail.com> >> >>> >> >> 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#StaticLoggerBinderfor >> >>> >> >> >> > 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.ja >> >>> >> >> >> > r >> >>> >> >> >> > >> >>> >> >> >> > 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-EntityMan >> >>> agerFactory-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* >> >>> >> >> > >> >>> >> ------------------------------------------------------------------- >> >>> >> >> >> >>> >> >> >>> >> >> >>