如何在eclipse中安装Hibernate Tools

Hibernate JBoss Tools Installation in Eclipse + Mapping Generation Example

Generating .hbm.cfg files or creating annotated files to map your classes is always a bit boring and it involves writing a lot of boilerplate code. In Eclipse you can use JBoss Hibernatate Tools, with which you can automatically generate all the files you need to work with Hibernate. In this tutorial we are going to see how to install Hibernate Tools in Eclipse and explore how you can generate mapping files automatically.

So these are the tools we are going to use on a Windows 7 platform:

  • JDK 1.7
  • Maven 3.0.5
  • Hibernate 4.2.3.Final
  • MySQL JDBC driver 5.1.9
  • Eclipse 4.3 Kepler

And this is the project I am going to use as the basis for the example : HibernateMySQLExample.zip. Download it and open it with Eclipse IDE.

JBoss Hibernate Tools Installation

In this example I’m using the brand new Eclipse IDE 4.3 Kepler. The same steps should apply basically on all versions of Eclipse.

1. Go to JBoss Hibernate Tools Web Site

Go to http://www.jboss.org/tools/download. And in the Downloads section click on the link that suits your IDE :

jboss-page

In the next window copy the repository URL. For my version of Eclipse that would be: http://download.jboss.org/jbosstools/updates/development/kepler/

link-repository

2. Install to Eclipse IDE

Now Open Eclipse IDE and go to Help -> Install New Software :

install-new-software

In the “Work with:” text field paste the repository URL and click Add.

add-resource

Then fill the form as shown in the picture below (You can put any name you like):

add-repository

After that, type “Hibernate” in the “type filter text” text field. It will bring up just what you need to work with Hibernate and not the full stack of JBoss Tools. If you want you can download them all but it will take much longer and not all of them are necessary for Hibernate:

hibernate-filter

Click Next. Accept the license and wait for the installion to finish. At some point you migh get a warning (very common when installing third party software on Eclipse). Just click OK and the installation will be continued:

warning

When the installation is completed you will be asked to restart the IDE. When the IDE is up again you will notice the new Hibernate Perspective. If it doesn’t show up click the button in the left of  the Java Perspective button :

perspective

And check weather Hibernate Perspective exists:

hibernate-perspective

Hibernate Code Generation

1. Hibernate configuration

Now open Hibernate Perspective and select “Add configuration”:

hibernate-perspective-windows

In the next Window:

add-config

  • In the “Project” text field click “Browse” and navigate to the Project you want to use.
  • In the “Configuration files” text field click Setup and choose “Use existing…”:

set-config-file

And the browse to the hibernate.cfg.xml of your project:

use-existing-config

  • In “Database Connections” you can click “New” if you want to create your own connection.

This is the hibernate.cfg.xml file we used to configure the connection:

hibernate.cfg.xml:

01 <?xml version="1.0" encoding="utf-8"?>
02 <!DOCTYPE hibernate-configuration PUBLIC
03 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
05 <hibernate-configuration>
06     <session-factory>
07         <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
08         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
09         <property name="hibernate.connection.username">root</property>
10         <property name="hibernate.connection.password"></property>
11         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tutorials</property>
12         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
13         <property name="show_sql">true</property>
14         <mapping resource="com/javacodegeeks/Student.hbm.xml"></mapping>
15     </session-factory>
16 </hibernate-configuration>

After that you ou can see all the databases and the tables you’ve created:

database-explorer

2. Code Generation

While in Hibernate Perspective click the “Hibernate Run” button and select “Hibernate Code Generation Configurations”:

code-gen-config

In the next window click on New Configuration:

new-configuration

In the "Exporters",我们可以生成POJO对象,.hbm.xml和.cfg.xml文件等。


In the “Output Direcotory” choose the folder you want Eclipse to put the generated files into. I’ve created a hibernateconfig folder inside the folder of the project. Make sure “Reverse Engineering from JDBC Connection” is checked. The click “Apply” and “Run”:

manage-config

Now, if you go back to the Package Exlorer you will see that Eclipse has generated mapping files and the corresponding classes for all the tables in the database:

generated-files


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值