Recent Posts

RSS Feeds

OpenJPA Class Enhancement with Maven

While attempting to put my time and energy where my mouth is I figured I'd take the time to make OpenJPA work in an example I've been doing of getting Spring/JPA/DbUnit to all play nicely together. I've got the example almost ready to post but not quite. Part of what has held me up is a couple of issues I was having getting the tests to work as I expected.

I was consistently getting the following errors:

testSingleParameterQuery(net.dudney.jpaund.dao.jpa.BaseDaoJpaTest)  Time elapsed: 0.11 sec  <<< FAILURE!
<4|true|0.9.6-incubating> org.apache.openjpa.persistence.ArgumentException: Could not locate metadata for the class using alias "Portfolio". Registered alias mappings: "{Portfolio=null}"
	at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:343)
	at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getClassMetaData(JPQLExpressionBuilder.java:164)
	at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.resolveClassMetaData(JPQLExpressionBuilder.java:142)
	at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getCandidateMetaData(JPQLExpressionBuilder.java:211)
	at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getCandidateMetaData(JPQLExpressionBuilder.java:181)
	at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getCandidateType(JPQLExpressionBuilder.java:174)
	at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.access$500(JPQLExpressionBuilder.java:61)
	at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder$ParsedJPQL.populate(JPQLExpressionBuilder.java:1657)
	at org.apache.openjpa.kernel.jpql.JPQLParser.populate(JPQLParser.java:52)
	at org.apache.openjpa.kernel.ExpressionStoreQuery.populateFromCompilation(ExpressionStoreQuery.java:145)
	at org.apache.openjpa.kernel.QueryImpl.newCompilation(QueryImpl.java:642)
	at org.apache.openjpa.kernel.QueryImpl.compilationFromCache(QueryImpl.java:623)
	at org.apache.openjpa.kernel.QueryImpl.compileForCompilation(QueryImpl.java:589)
	at org.apache.openjpa.kernel.QueryImpl.compileForExecutor(QueryImpl.java:651)
	at org.apache.openjpa.kernel.QueryImpl.getOperation(QueryImpl.java:1464)
	at org.apache.openjpa.kernel.DelegatingQuery.getOperation(DelegatingQuery.java:120)
	at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:202)
	at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:251)
	at net.dudney.jpaund.dao.jpa.BaseDaoJpa.find(BaseDaoJpa.java:97)
	at net.dudney.jpaund.dao.jpa.BaseDaoJpa.find(BaseDaoJpa.java:74)
	at net.dudney.jpaund.dao.jpa.BaseDaoJpa.find(BaseDaoJpa.java:109)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:281)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:199)
	at $Proxy9.find(Unknown Source)
	at net.dudney.jpaund.dao.jpa.BaseDaoJpaTest.testSingleParameterQuery(BaseDaoJpaTest.java:172)

testSaveOrUpdateObjectNewObject(net.dudney.jpaund.dao.jpa.BaseDaoJpaTest)  Time elapsed: 0.233 sec  <<< FAILURE!
org.springframework.dao.InvalidDataAccessApiUsageException: The type "class net.dudney.jpaund.domain.VersionedObject" has not been enhanced.; nested exception is <4|true|0.9.6-incubating> org.apache.openjpa.persistence.ArgumentException: The type "class net.dudney.jpaund.domain.VersionedObject" has not been enhanced.
Caused by: <4|true|0.9.6-incubating> org.apache.openjpa.persistence.ArgumentException: The type "class net.dudney.jpaund.domain.VersionedObject" has not been enhanced.
	at org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java:1590)
	at org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1564)
	at org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:654)
	at org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository.java:554)
	at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:479)
	at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:283)
	at org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository.java:519)
	at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:479)
	at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:283)
	at org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository.java:519)
	at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:479)
	at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:283)
	at org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:233)
	at org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:97)
	at org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3124)
	at org.apache.openjpa.kernel.DelegatingBroker.attach(DelegatingBroker.java:1120)
	at org.apache.openjpa.persistence.EntityManagerImpl.merge(EntityManagerImpl.java:591)
	at org.springframework.orm.jpa.JpaTemplate$6.doInJpa(JpaTemplate.java:272)
	at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:191)
	at org.springframework.orm.jpa.JpaTemplate.merge(JpaTemplate.java:270)
	at net.dudney.jpaund.dao.jpa.BaseDaoJpa.update(BaseDaoJpa.java:118)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:281)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:187)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
	at $Proxy9.update(Unknown Source)
	at net.dudney.jpaund.dao.jpa.BaseDaoJpaTest.testSaveOrUpdateObjectNewObject(BaseDaoJpaTest.java:121)

Which of course lead me to look into how one enhances for OpenJPA with Maven. Which lead me here. Unfortuanately that leaves off the important bit that you have to attach the plugin to a phase (I expected it was attached to process-classes by default, but as you know we should never as-u-me anything:). So anyway after another 20 minutes of googling around to here and here. It finaly dawned on me that my classes were not actually enhanced. So I updated my pom to look like this;

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>openjpa-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>JPA Enhance</id>
      <phase>process-classes</phase>
      <goals>
        <goal>enhance</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <toolProperties>
      <property>
        <name>addDefaultConstructor</name>
        <value>true</value>
      </property>
      <property>
        <name>enforcePropertyRestrictions</name>
        <value>true</value>
      </property>
    </toolProperties>
  </configuration>
</plugin>

And bada-boom bada-bing it started working like a champ.

I post this so that others that might be struggeling with the same frustration might find a quicker solution.

Permalink     4 Comments



Comments:

This should no longer be required to get started; OpenJPA can now work with unenhanced classes, albeit with a few restrictions. There is still some ongoing work happening on the feature, but it seems to be pretty functional at this point.

-Patrick

Posted by Patrick Linskey on July 27, 2007 at 01:06 AM MDT #

Thanks Patrick, I'll grab the latest and check it out.

Posted by Bill Dudney on July 27, 2007 at 09:29 AM MDT #

Running into the same problems as you did Bill I want to confirm Patricks valuable post. Updating to OpenJPA 1.0.0 solved the issue for me.

-Toddy

Posted by Toddy on October 03, 2007 at 03:55 PM MDT #

Hi Toddy,

I got much better results from 1.0.0 and did not have to enhance. However I ended up going back to enhancement because of some warnings that OpenJPA was printing to the console. I used the ant-run plugin though.

Posted by Bill Dudney on October 03, 2007 at 04:02 PM MDT #

Post a Comment:
  • HTML Syntax: Allowed