Search code examples
hibernateglassfishjpa-2.0

How can we use JPA2 (Hibernate) entities as DTO's between two webapps?


We're running on Glassfish 3.0.1 and using Hibernate 3.5.3. Our project setup looks like this:

  • frontend.war
  • common.jar
  • backend.war

We would like to put our jpa2 annotated entities in common.jar in such a way that the backend treats these as JPA2 entities, but the frontend should only see these as POJO's/DTO's. We thought this could be accomplished bu putting persistence.xml in backend.war and having no persistence.xml in the frontend. This doesn't work, after starting up the backend and calling entityManager.getMetamodel().getEntities(), we get an empty list. All queries fail with exeptions: "Not an entity: com.example.model.OurEntity".

We have tried both with and without beans.xml in common.jar.

Any idea what we are doing wrong? Is it possible to use this structure with JPA2?


Solution

  • When annotated entities are located not in the same file as persistence.xml, you need to add <jar-file> to persistence.xml. Depending on your setup it can look like this:

    <jar-file>lib/common.jar</jar-file>