Hibernate Tools Eclipse 插件安装
博客分类:- ?
- Hibernate
?
Hibernate Tools 简介:
??? Hibernate Tools是由JBoss推出的一个Eclipse综合开发工具插件,该插件可以简化ORM框架Hibernate,以及JBoss Seam,EJB3等的开发工作。Hibernate Tools可以以Ant Task以及Eclipse插件的形式运行。
?
Mapping Editor(映射文件编辑器):该编辑器可以用来编辑Hibernate XML映射文件,提供代码自动补全,语法高亮功能。?
???? - 类名的自动补全?
???? - 属性/表的列名自动补全?
Hibernate Console(Hibernate控制台):它提供一个全新的Eclipse Perspective窗口,可以执行HQL,并查看查询的结果。这是一个非常便利的工具,可以用来验证HQL的语法是否正确。?
Configuration Wizards and Code generation(Hibernate配置文件hibernate.cfg.xml创建导航,以及代码生成器):支 持完全反向生成功能,可以从已有的数据库生成Hibernate配置文件hibernate.cfg.xml,同时支持"click-and- generate"方式生成Java代码(Hibernate POJOs),DAO,XML映射文件(.hbm.xml),以及schema 文档(.hml)等文件。?
Eclipse JDT integration(代码补全):该工具支持Java代码中的HQL输入补全,以及对Java代码中的HQL语法加以合法性验证。
?
下面是Hibernate Tools的相关使用及配置:
1.到Hibernate的官网上下载?
HibernateTools-3.2.4.GA-R200905070146-H18.zip?
2.解压下载的文件,可以得到两个文件夹features和plugins,把这两个文件夹里边的东西分别考贝到Eclipse的features和plugins下。?
3.验证一下是不是安装好了。选择工程下的SRC目录,然后右键New->Other->Hibernate?
看到以下内容就说明安装成功了
?
?
?4.现在我们可以选择其中的某一项来使用了。先建一个hibernate.cfg.xml吧。如图:
?
自动创建的文件内容如下:
- <?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>??
- ????????<property?name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>??
- ????????<property?name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>??
- ????????<property?name="hibernate.connection.username">root</property>??
- ????????<property?name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>??
- ????</session-factory>??
- </hibernate-configuration>??
??在这个基础上,我们可以自行添加我们需要的property和mapping。
下面是一个相对比较完整的版本:
- <?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>??
- ????????<!--??Database?Connection?Settings?-->??
- ????????<property?name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>??
- ????????<property?name="hibernate.connection.url">jdbc:mysql://localhost:3306/UniversityDatabase</property>??
- ????????<property?name="hibernate.connection.username">root</property>??
- ????????<property?name="hibernate.connection.password"></property>??
- ????????<property?name="hibernate.default_schema">UniversityDatabase</property>??
- ????????<!--?SQL?dialect?-->??
- ????????<property?name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>??
- ??
- ????????<!--?JDBC?connection?pool?(use?the?built-in)?-->??
- ????????<property?name="connection.pool_size">1</property>??
- ??
- ????????<!--?Enable?Hibernate's?automatic?session?context?management?-->??
- ????????<property?name="current_session_context_class">thread</property>??
- ??
- ????????<!--?Disable?the?second-level?cache??-->??
- ????????<property?name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>??
- ??
- ????????<!--?Echo?all?executed?SQL?to?stdout?-->??
- ????????<property?name="show_sql">true</property>??
- ??
- ??
- ????????<mapping?resource="User.hbm.xml"?/>??
- ????????<mapping?resource="Course.hbm.xml"?/>??
- ????????<mapping?resource="Employee.hbm.xml"?/>??
- ??
- ????</session-factory>??
- ??
- </hibernate-configuration>??
?5.建一个Hibernate Console Configuration,这里需要给Configuration起个名字,Project项目中选择当前工程,然后Datasbase Connection选择连接的数据库,单击OK。如图:
?
?
如果成功的话,会在Hibernate的控制台看到数据库中的Table列表,如图:
?
6.新建hibernate.reveng.xml
new->others->hibernate->Hibernate Reverse Engineering File到了以下画面:选择console configuration,点击refresh,选中需要Reverse的Table,点击include。最后finish就好了。
?
生成的XML如下:
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <!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>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="admin"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="course"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="department"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="employee"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="employeeaddresses"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="employeecertification"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="employeeeducations"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="employeehours"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="employeememo"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="externalcourse"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="internalcourse"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="person"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="project"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="resourceassignment"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="student"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="user"/>??
- ??<table-filter?match-catalog="UniversityDatabase"?match-name="usercourses"/>??
- </hibernate-reverse-engineering>??