JPA and Groovy

I have recently been playing with JPA, TopLink and Java DB (Apache Derby if you prefer) to manage date in Java SE applications. And I love those tools! It then dawned to me that I could make things even easier by using Groovy.

Unfortunately, TopLink doesn’t seem to play nice with Groovy as none of my persistent classes can be found by TopLink. I have tried to write the POJO in Groovy, write the POJO in Groovy and compile a .class and finally write the POJO in Java but nothing worked. TopLink keep sending me a ClassNotFoundException at startup. TopLink finds the persistence.xml file so it’s not the problem. Incidentally, all my Groovy code works just fine when compiled and ran with javac/java.

Does any of you every tried to use TopLink with Groovy? If so, I’d love to get some help. (And I don’t want to use Hibernate’s JPA implementation by the way :-)

Update: the source code of both Java and Groovy version is available. The Groovy version cannot find the Person POJO no matter what I do (use a Groovy bean, compile the Groovy source to a .class, use a Java bean, use a Java bean in a JAR, etc.)

15 Responses to “JPA and Groovy”

  1. Doug Clarke says:

    I would be more then happy to assist. Are you listing the class in the persistence unit definition in the persistence.xml?

    I am not a Groovy expert but if you sent me your example I would happy to try it out and see if I can point you in the right direction.


  2. Romain Guy says:

    Hi Doug! Thanks for offering your help. The class is indeed listed in the persistence.xml. In fact, this Groovy test is a port of a working Java test. I’ll send you both versions asap.

  3. Romain Guy says:

    I have updated this blog entry with a pointer to the source.

  4. Not a huge Grovy expert, but I’m fairly certain that Groovy lacks support for annotations so my guess is that’s why your persistence annotations are ignored.

    I removed the Person tag and added a basic orm.xml in the META-INF directory. This is the content of my orm.xml:

    Running the populate script did now work fine and there were 9 rows in the person table:

    CONNECTION0* – jdbc:derby:groovy/address-book
    * = current connection
    ij> select count(*) as count from person;

    Hope this helps,

  5. Looks like your blog softeware ate my xml – I’ll try again after escaping it. This is the orm.xml that I used:

    <?xml version=”1.0″ encoding=”UTF-8″?>

    <entity-mappings xmlns=””

    <entity class=”Person” metadata-complete=”true” access=”FIELD”>
    <id name=”id”>
    <generated-value strategy=”AUTO”/>
    <basic name=”firstName”/>
    <basic name=”lastName”/>


  6. The orm.xml didn’t make it into the post – try this link to download it.

  7. I wasn’t quite clear above – I had to remove the Person class reference from the peristence XML file to get it to work.

  8. Not so sure it is the lack of annotation support that is the problem. Groovy 1.0 doesn’t work with the annotations but built a current snapshot of Groovy 1.0.1 and now the JPA annotations are picked up. I still need a minimal entry in orm.xml to get the class to be found – probably a classloading issue. If I use this

    <entity class=”Person” name=”Person” metadata-complete=”false” access=”FIELD”>

    in orm.xml instead of referencing the class in persistence.xml everything works OK.

  9. Romain Guy says:

    Thomas, thanks a lot! I was indeed using a snapshot of Groovy that does support annotations, which is not Groovy 1.0. What is orm.xml? How is it different from specifying entries in persistence.xml? I’ll use this until a fix is found in either Groovy and/or TopLink though. Thanks again!

  10. Romain Guy says:

    Thomas, with your minimal version, I get another error. TopLink complains about not finding any @Id in the Person class.

  11. Romain Guy says:

    Never mind, my bad. I messed up with Groovy’s snapshots. Everything is fine.

  12. Romain, glad you got it to work. The orm.xml file is a way to externalize your configuration and override all or some of your annotations so they can be changed without having to recompile your code. This is of course a moot point when you are using Groovy :) You can have multiple orm.xml files and specify the names in the persistence.xml file. I have no idea why specifying the class in the orm.xml file works while specifying it in persistence.xml doesn’t. Hopefully someone from the TopLink project can shed some light here.

  13. James Chua says:

    Hi Romain,

    May I know why you don’t want to use hibernated with JPA?


  14. Romain Guy says:


    Hibernate was too much of a pain to configure. Just figuring out what jars to put in my application drove me away. TopLink was a single jar. Besides, why Hibernate and not TopLink? :)

  15. Sammie Quinn says: