-
1. Re: how can I reverse engineer and create POJOs from an existing database?
johnqcitizen Mar 7, 2013 11:57 PM (in response to gtludwig)1 of 1 people found this helpfulYou 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 POM 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), and the package name, etc.
You will also need to define a src/main/resources/hibernate.properties 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 driver_class, url, username, password.
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.xml file 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. It mostly works for me (I am having one issue with an oracle schema).
Hope that helps.
-
2. Re: how can I reverse engineer and create POJOs from an existing database?
maxandersen Mar 8, 2013 5:16 AM (in response to gtludwig)http://docs.jboss.org/tools/latest/en/hibernatetools/html_single/index.html is a good start
If your database does not have foreign key assocations there are no info available to make assocations. That is why you need a reveng.xml to describe these assocations.
-
3. Re: how can I reverse engineer and create POJOs from an existing database?
shanthraj92 Jul 7, 2017 2:08 AM (in response to maxandersen)Hello Max,
I was able to generate Java POJOs for entities, but mapping (OneTo One/ManyToOne/OneToMany/ManyToMany)between the generated entities are not proper. kindly do the need full.