Use Hibernate Tools to generate Entity classes from Tables
1. Introduction
This document is based on:
Eclipse 4.5 (MARS)
Hibernate 5.1.0.Final
Hibernate Tools 4.3.1.Final.html
The first we must be make sure you have make sure that installed "Hibernate Tools" on Eclipse. "Hibernate Tools" is one tool in the tools of JBoss -. "JBoss Tools".
See more:
2. Database for example, used in this document
The database used to illustrate in this tutorial is "simplehr". You can refer to:
3. Connect to the database via the "Database Development"
In Eclipse select:
- Window/Perspective/Open Perspective/Other...
Create a new connection to the "simplehr" database:
In this tutorial I will connect to Oracle database. You can connect to a different database, entirely similar way.
Press to the icon, to declare Driver for your selected database.
Click to "Add JAR/Zip"
Enter the information to connect to the database. Then click "Test Connection" to ensure that the database connection is successful.
You can see the structure of your database in the "Data Source Explorer".
Try to query data in a table.
4. Create Project
Enter:
- Group Id: org.o7planning
- Artiface Id: HibernateGenerateEntities
Project is created:
Declaring Hibernate 5 library, and JDBC Driver for Oracle, MySQL, SQL Server:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.o7planning</groupId>
<artifactId>HibernateGenerateEntities</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HibernateGenerateEntities</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<!-- Repository for ORACLE JDBC Driver -->
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Hibernate -->
<!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.1.0.Final</version>
</dependency>
<!-- MySQL JDBC driver -->
<!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<!-- Oracle JDBC driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<!-- SQLServer JDBC driver (JTDS) -->
<!-- http://mvnrepository.com/artifact/net.sourceforge.jtds/jtds -->
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
5. Configuring Hibenate
"Hibernate Tools" allows create configuration file. And now we will create the file "hibernate.cfg.xml" using this tool.
In Eclipse, select:
- Window/Perspective/Open Perspective/Other...
Right-Click, and select "Add Configuration .." to create Hibernate configuration file.
Enter the file name "hibernate.cfg.xml", and put it into the "src/main/java" folder of the Project.
You can type the information into a database connection or retrieve the connection information that you have previously declared. Click on "Get values from Connection".
Select Connection "Oracle (Simplehr)", which you have created above. Click OK to continue.
Next select the class "dialect" that corresponds to your database type. With Oracle is Oracle10g, attention dialect class "Oracle10g" is used for both versions 10 and 11 of Oracle.
Click OK to complete the configuration. After this step will file "hibernate.cfg.xml" was created in the src/main/java folder of your Project.
Hibernate Configuration is created. (See in Hibernate Configurations view)
See in "Package Explorer":
- hibernate.cfg.xml
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.password">12345</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:db12c</property>
<property name="hibernate.connection.username">simplehr</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
</session-factory>
</hibernate-configuration>
Next we will configure, how to read the table structure and generate to the entity class
- Window/Perspective/Open Perspective/Hibernate
Note: If the process of generating the entities fails, you can see how to solve in the appendix.
6. Appendix: Error while generating the entities
Maybe when generating the entities you get an error as follows:
** NoClassDefFoundError **
org.hibernate.console.HibernateConsoleRuntimeException: Received a NoClassDefFoundError,
probably the console configuration classpath is incomplete or contains conflicting versions of the same class
Received a NoClassDefFoundError, probably the console configuration classpath is incomplete or contains conflicting versions of the same class
org.hibernate.console.HibernateConsoleRuntimeException:
Received a NoClassDefFoundError, probably the console configuration classpath is incomplete or contains conflicting versions of the same class
Received a NoClassDefFoundError, probably the console configuration classpath is incomplete or contains conflicting versions of the same class
java.lang.NoClassDefFoundError: org/apache/commons/collections/MultiMap
org/apache/commons/collections/MultiMap
java.lang.ClassNotFoundException: org.apache.commons.collections.MultiMap cannot be found by org.jboss.tools.hibernate.runtime.v_5_1_5.0.1.Final-v20160331-1852-B88
org.apache.commons.collections.MultiMap cannot be found by org.jboss.tools.hibernate.runtime.v_5_1_5.0.1.Final-v20160331-1852-B88
You need to configure to use an older version of Hibernate.
And rerun to generate the entities: