Reuse JPA entity classes from a Jar file


Let’s say you had your application in one single Maven project as a prototype and decided to finalize the functionality into a finished (or almost, e.g., Alpha or Beta version) product. When you have JPA entities and you separated them into a single jar file, your persistence.xml needs to change and use the <class> tag in that file.
Of course, the JPA entities jar file has to be in the classpath.

Software Environment

  • Windows 7 Professional SP1
  • Eclipse – Kepler Release
  • Java 1.7 (1.7.0_67 – Windows x86)
  • MySQL 5.6.16 – Community Server (GPL)
  • MySQL Java Connector 5.1.34
  • Hibernate 4.3.7 (Entity Manager)

JPA Entity Class

Your entities can keep their JPA-related annotations. Here is a sample entity class:

Modify persistence.xml

Although your classes may be in a separate jar file, they are still in the same application and it referenced in persistence.xml using <class> tags.

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert