JBoss Community

Re: how can I reverse engineer and create POJOs from an existing database?

created by John Citizen in JBoss Tools - View the full discussion

You don't have to use eclipse plugins here. You can also use the hibernate plugin for maven, and generate mapping files and POJOs from the command line.

 

You would define a pom.xml file as follows (I've left out some of the normal POMN stuff here) :

 

<project>

  <build>

    <plugins>

     <plugin>

       <groupId>org.codehaus.mojo</groupId>

       <artifactId>hibernate3-maven-plugin</artifactId>

       <version>2.2</version>

         <configuration>

           <components>

             <component>

               <name>hbm2hbmxml</name>

               <implementation>jdbcconfiguration</implementation>

               <outputDirectory>target/generated-resources/hibernate3</outputDirectory>

             </component>

             <component>

               <name>hbm2java</name>

               <implementation>jdbcconfiguration</implementation>

               <outputDirectory>target/generated-sources/hibernate3</outputDirectory>

             </component>

           </components>

           <componentProperties>

             <revengfile>drc/main/resources/reveng.xml</revengfile>

             <propertyfile>src/main/resources/hibernate.properties</propertyfile>

             <packagename>com.whatever.domain</packagename>

             <jdk5>true</jdk5>

             <ejb3>true</ejb3>

           </componentProperties>

         </configuration>

         <dependencies>

           <dependency>

             <groupId>cglib</groupId>

             <artifactId>cglib-nodep</artifactId>

             <version>2.2.2</version>

           </dependency>

           <dependency>

             <groupId>com.oracle</groupId>

             <artifactId>ojdbc</artifactId>

             <version>11.1.0.6.0</version>

           </dependency>

         </dependencies>

       </plugin>

    </plugin>

  </build>

</project>

 

You will need to change the values for the JDBC dependency (I'm using oracle).

 

You will also need to define a src/main/resources/hibernate.propeties file:

 

hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver

hibernate.connection.url=jdbc:oracle:thin:@<server>:<port>:<instance>

hibernate.connection.username=<username>

hibernate.connection.password=<password>

 

using suitable values for <server>, <port>, etc.

 

And a src/main/resources/reveng.xml file to define your reverse engineering strategy, e.g.

 

?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-reverse-engineering PUBLIC

  "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"

  "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

 

<hibernate-reverse-engineering>

  <schema-selection match-schema="MY_SCHEMA"/>

</hibernate-reverse-engineering>

 

This specifies that only tables in the MY_SCHEMA schema are used for generating POJOs.

 

Then run the following from the command line to generate POJOs with annotations:

 

$ mvn hibernate3:hbm2java

 

If you want to generate mapping files and POJOs without annotations, change the <ejb3> tag in the pom.xm to the following:

 

  <ejb3>false</ejb3>

 

then run the following:

 

  $ mvn hibernate3:hbm2hbmxml

   $ mvn hibernate3:hbm2java

 

This is just a taster. You will need to look up additional information to get this working.

 

Hope that helps.

Reply to this message by going to Community

Start a new discussion in JBoss Tools at Community