Generate hibernate model class and mapping files automatically

Generate hibernate model class and mapping files automatically

Any hibernate project you are working on need hibernate related model class, DAO class, mapping files etc. If some dedicated resource is generating this files for the project then it’s very easy to use and write code. But when you get this responsibility first time and don’t know anything about generating these files automatically then what we do is start writing this file manually using old file copy paste and modify it accordingly. JBoss tools has given us tool name hibernate tool to generate this files automatically. Please follow below steps to generate files automatically using JBoss tools:

Tools needed:

Below are steps: 

1. Create table in MySQL data base script:

DROP TABLE address
GO

CREATE TABLE address ( 
	Steet_Name	varchar(50) NULL,
	Location  	varchar(25) NULL,
	City      	varchar(25) NULL,
	Zip       	varchar(25) NULL,
	State     	varchar(25) NULL,
	id        	int(11) NOT NULL DEFAULT '0' 
	)
GO

ALTER TABLE address
	ADD PRIMARY KEY (id)
GO

 

2. Eclipse setup:

  • Open your eclipse
  • Create Maven project name: HibernateMySQLTest (If you are not sure how to create Maven project in eclipse please click this tutorial)
  • By default eclipse kepler creates dynamic maven project of version 2.3. If you try to change version to 2.5 through Right click project –>Properties –> Project Facet will give you an error and if you are getting error then click Window –> Show view –> Other –> General –> Navigator –> Click OK
  • Eclipse will change in Navigator perspective. Click .setting folder — and open file name Open org.eclipse.wst.common.project.facet.core.xml file then make change show below:
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
  <fixed facet="wst.jsdt.web"/>
  <installed facet="java" version="1.5"/>
  <installed facet="jst.web" version="2.5"/>
  <installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>

 

  • This will change maven dynamic project version 2.5.
  • Now create resources folder inside src/main.
  • Create package com.javahonk and com.javahonk.bean and com.javahonk.util
  • Project structure shown below:

Generate hibernate model class and mapping files automatically

3. Hibernate configuration:

  • Click Window –> Show View –> Others –> Choose Hibernate –> Hibernate Configuration

Generate hibernate model class and mapping files automatically

 

  • This will open Hibernate Configuration perspective. Right click –> click Add Configuration
  • Choose as below:
    • Project : Browse and select project
    • Database connection: Click New

Generate hibernate model class and mapping files automatically

  • Choose MySQL — Click Next

Generate hibernate model class and mapping files automatically

  • Next window specify driver and connection details for MySQL database. Sample shown below:

Generate hibernate model class and mapping files automatically

  • Before click next you to complete one more step to add jar for MySQL JDBC driver. For this click New driver definition as below:

Generate hibernate model class and mapping files automatically

  • Choose MySQL version 5.1 then click JAR List and add mysql-connector-java-5.1.0-bin.jar (This jar is already include in project so you can download it from bottom)

Generate hibernate model class and mapping files automatically

  • On JAR List window — Click Add JAR/Zip to add jar then click OK

Generate hibernate model class and mapping files automatically

  • Please test connection before click next. To test connection click Test Connection. If everything configured correctly it will show you ping successful window as below:

Generate hibernate model class and mapping files automatically

  • Now on Edit launch configuration properties window on Configuration file box –> Click set up button to create new or use existing file as sample below:

Generate hibernate model class and mapping files automaticallyGenerate hibernate model class and mapping files automatically

  • Click OK. Now you will see list of table in Hibernate perspective as below:

Generate hibernate model class and mapping files automatically

  • Sample generated hibernate.cfg.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="sessionFactory">
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">admin</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/JavaHonk</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.default_catalog">JavaHonk</property>
        <property name="hibernate.default_schema">JavaHonk</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    </session-factory>
</hibernate-configuration>

4. Generate hibernate model class and mapping files automatically

  • Click window –> Open perspective –> Others –> Hibernate
  • Click Run new configuration — Click Hibernate Code generation configuration

Generate hibernate model class and mapping files automatically

  • This will open Create, manage, and run configuration window
    • Console configuration : Choose choose which we configured already in above step
    • Output directory: Browse and choose output directory where you want generate files
    • Check box Reverse engineer form JDBC Connection
    • Sample shown below

Generate hibernate model class and mapping files automatically

Now Click Exporters tab and select whatever exporter you needed. We have chosen Domain code and hibernate mappings file as shown below:

Generate hibernate model class and mapping files automatically

  • Now click Run. This will generate Address.hbm.xml and Address.java model class in resources folder. As you remember in our very first step we had created table name address and tools generated corresponding file as show below:

Generate hibernate model class and mapping files automatically

  • That’s it.

Leave a Reply

Your email address will not be published. Required fields are marked *