Hibernate provides a tool called “Hibernate Reverse Engineering” that allows you to generate Java entity classes and Hibernate mapping files from an existing database schema. The process involves using the Hibernate Tools or the Hibernate Reverse Engineering Tool.
Using Hibernate Tools:
Add Required Libraries: Make sure you have the Hibernate libraries in your project. This includes the Hibernate Core library, JDBC driver for your database, and any other dependencies. Add this in your pom.xml.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-tools</artifactId>
<version>5.6.15.Final</version>
</dependency>
Create Hibernate Configuration File: Create a Hibernate configuration file (e.g., hibernate.
properties) with database connection details and other required configurations. An example is provided in the previous responses.
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class=org.postgresql.Driver
hibernate.connection.url=jdbc:postgresql://localhost:5432/post
hibernate.connection.username=postgres
hibernate.connection.password=postgres
hibernate.default_schema=demo
Add Hibernate Tools Dependency: Add the Hibernate Tools dependency to your project. If you are using Maven, you can add the following dependency. Also do not forget to add dependencies related to you DB that you need Entities of.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2java</name>
<outputDirectory>src/main/java/</outputDirectory>
<implementation>jdbcconfiguration</implementation>
</component>
</components>
<componentProperties>
<revengfile>/src/main/resources/hibernate.reveng.xml</revengfile>
<propertyfile>/src/main/resources/hibernate.properties</propertyfile>
<jdk5>true</jdk5>
<ejb3>true</ejb3>
</componentProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>compile</phase>
<goals><goal>hbm2java</goal></goals>
</execution>
</executions>
</plugin>
Add Reverse Engineering Properties File : Add this “hibernate.reveng.xml” in same path as your hibernate.properties file is.
<!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="demo"/>
<type-mapping>
<sql-type jdbc-type="VARCHAR" hibernate-type="java.lang.String"/>
<sql-type jdbc-type="INTEGER" hibernate-type="java.lang.Integer"/>
<sql-type jdbc-type="BIGINT" hibernate-type="java.lang.Long"/>
<sql-type jdbc-type="TIMESTAMP" hibernate-type="java.sql.Timestamp"/>
<sql-type jdbc-type="NUMERIC" hibernate-type="java.math.BigDecimal"/>
<sql-type jdbc-type="BINARY" hibernate-type="byte[]"/>
<sql-type jdbc-type="SQLXML" hibernate-type="java.sql.SQLXML"/>
<!--Add More SQL-Types per your need but not necessary -->
</type-mapping>
<table-filter match-name=".*" match-schema="demo"/>
<table name=".*"/>
</hibernate-reverse-engineering>
Now run your maven command – mvn clean compile and you should see your entity class getting created. Easy.
Leave a Reply