How to get hibernate tools eclipse plugin to generate Hibernate mapping files and create entity from Database?

1) Firstly , Go to window tab > Perspective > Other..> Hibernate.
2) if the Plugin does not exist, go to tab Help > Choose Eclipse Marketplace > find : “Hibernate Tools“,
then choose the result – “jboss tool 4.3.0 Final“, then install…

image_1

3) If done install, go to window tab again > Perspective > Other..> Hibernate. then Click ok Button.
image_2.jpg

4) Setup your new Hibernate Configuration
In Hibernate Perspective, right click and select “Add Configuration…

image_3.jpg
5) In “Edit Configuration” dialog box, click browse to select your project.
image_4.jpg

6) After that , select for the option – “Database Connection” box, click “New..” button to create your database settings.

– choose mysql DB, then select any Mysql Driver. – if missing jar file, download this jar –mysql-connector-java-5.1.38.jar for
connector.

image_5
7) In “Configuration File” box, click “Setup” button to create a new or use existing “Hibernate configuration file”,
it will automatic generate file for you. And put in package src\main\resources for the file – hibernate.cfg.xml.
image_6

image_7.jpg

image_8

– click Finish
– Apply > OK

8) the result of your hibernate config file – hibernate.cfg.xml.
image_9

9) generate the Hibernate mapping files and annotation codes.
image_10.jpg

10) Create a new configuration, select your “console configuration” , puts your “Output directory” and checked option “Reverse engineer from JDBC Connection“.
image_11
11) In “Exporter” tab, select what you want to generate, Model , mapping file (hbm) , DAO, annotation code and etc.

image_12.jpg

  • Please select option “use Java 5 syntax”.

10) Then it will automatically generate the entity class and hbm.xml

image_14.jpg

11) then check the hibernate.cfg.xml, which we should add the mapping for stock.hbm.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”&gt;
<hibernate-configuration>
<session-factory>
<property name=”hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=”hibernate.connection.password”>P@ssw0rd</property>
<property name=”hibernate.connection.url”>jdbc:mysql://localhost:3306/hibernate</property>
<property name=”hibernate.connection.username”>root</property>
<property name=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>
<property name=”show_sql”>true</property>
<mapping resource=”com/fndong/common/Stock.hbm.xml”></mapping>
</session-factory>
</hibernate-configuration>

 

11) What is the <property name=”show_sql“>true</property> for ?

  • answer: Enable the logging of all the generated SQL statements to the console

12) More info from other’s site: 

http://o7planning.org/web/fe/default/en/document/7758/using-hibernate-tools-generate-entity-classes-from-tables

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s