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" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="foo"> <!-- only used to reverse the db --> <properties> <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"/> </properties> </persistence-unit> </persistence>
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:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <mainClass>org.apache.openjpa.jdbc.meta.ReverseMappingTool</mainClass> <arguments> <argument>-directory</argument><argument>src/main/java</argument> <!-- or target/generated/model --> <argument>-accessType</argument><argument>fields</argument> <argument>-useGenericCollections</argument><argument>true</argument> <argument>-package</argument><argument>org.superbiz.model</argument> <argument>-innerIdentityClasses</argument><argument>false</argument> <argument>-useBuiltinIdentityClass</argument><argument>false</argument> <argument>-primaryKeyOnJoin</argument><argument>false</argument> <argument>-annotations</argument><argument>true</argument> <argument>-p</argument><argument>src/main/resources/META-INF/reverse-persistence.xml</argument> </arguments> </configuration> </plugin>
Note: you can need to add either as plugin dependencies or as project dependencies openjpa (+ dependencies), bean validation api, and mysql driver:
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.24</version> </dependency> <dependency> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-validation_1.0_spec</artifactId> <version>1.1</version> </dependency> </dependencies>
Execute the plugin (mvn exec:java) and you should find your JPA model in src/main/java/[the package specified in the plugin].
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
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.