OpenJPA: reverse tool

When starting an application with an existing database we often look for a reverse tool to generate JPA entities from the database. OpenJPA provides this kind of tool and it is easily integrable with maven.

Here is the way to do so.

First create a persistence.xml (i’ll call it reverse-persistence.xml) with the connection properties:

<?xml version="1.0"  encoding="UTF-8"?>
<persistence version="2.0"
  <persistence-unit  name="foo"> <!-- only used to reverse the db -->
      <property name="openjpa.ConnectionUserName" value="aaa"/>
      <property name="openjpa.ConnectionPassword" value="bbb"/>
      <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/ccc"/>
      <property name="javax.persistence.jdbc.driver"  value="com.mysql.jdbc.Driver"/>

Then simply call the main org.apache.openjpa.jdbc.meta.ReverseMappingTool available in openjpa jar. To do so with maven you can use the maven exec plugin:

      <argument>-directory</argument><argument>src/main/java</argument> <!-- or target/generated/model -->

Note: you can need to add either as plugin dependencies or as project dependencies openjpa (+ dependencies), bean validation api, and mysql driver:


Execute the plugin (mvn exec:java) and you should find your JPA model in src/main/java/[the package specified in the plugin].


2 thoughts on “OpenJPA: reverse tool

  1. arunkuttimar

    Hi dudes..This is fine ..But i have an another challenge..i’m working with zero xml configuration (pure annotation config) how to get the entity classes without persistence.xml ..Thanks in advance

    1. rmannibucau Post author

      There is no standard way to do it with JPA. With OpenJPA/Hibernate direct API you can do it but I wouldnt try to keep my app tolerant to version updates.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s