当前位置: 代码迷 >> Eclipse >> Hibernate Tools Eclipse 插件装配
  详细解决方案

Hibernate Tools Eclipse 插件装配

热度:25   发布时间:2016-04-23 01:16:09.0
Hibernate Tools Eclipse 插件安装

Hibernate Tools Eclipse 插件安装

博客分类:
    ?
  • Hibernate
HibernateEclipseMySQLXMLJDBC?

?

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代码??收藏代码
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <!DOCTYPE?hibernate-configuration?PUBLIC??
  3. ????????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"??
  4. ????????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">??
  5. <hibernate-configuration>??
  6. ????<session-factory>??
  7. ????????<property?name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>??
  8. ????????<property?name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>??
  9. ????????<property?name="hibernate.connection.username">root</property>??
  10. ????????<property?name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>??
  11. ????</session-factory>??
  12. </hibernate-configuration>??

??在这个基础上,我们可以自行添加我们需要的property和mapping。

下面是一个相对比较完整的版本:

Xml代码??收藏代码
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <!DOCTYPE?hibernate-configuration?PUBLIC??
  3. ????????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"??
  4. ????????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">??
  5. ??
  6. <hibernate-configuration>??
  7. ??
  8. ????<session-factory>??
  9. ????????<!--??Database?Connection?Settings?-->??
  10. ????????<property?name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>??
  11. ????????<property?name="hibernate.connection.url">jdbc:mysql://localhost:3306/UniversityDatabase</property>??
  12. ????????<property?name="hibernate.connection.username">root</property>??
  13. ????????<property?name="hibernate.connection.password"></property>??
  14. ????????<property?name="hibernate.default_schema">UniversityDatabase</property>??
  15. ????????<!--?SQL?dialect?-->??
  16. ????????<property?name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>??
  17. ??
  18. ????????<!--?JDBC?connection?pool?(use?the?built-in)?-->??
  19. ????????<property?name="connection.pool_size">1</property>??
  20. ??
  21. ????????<!--?Enable?Hibernate's?automatic?session?context?management?-->??
  22. ????????<property?name="current_session_context_class">thread</property>??
  23. ??
  24. ????????<!--?Disable?the?second-level?cache??-->??
  25. ????????<property?name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>??
  26. ??
  27. ????????<!--?Echo?all?executed?SQL?to?stdout?-->??
  28. ????????<property?name="show_sql">true</property>??
  29. ??
  30. ??
  31. ????????<mapping?resource="User.hbm.xml"?/>??
  32. ????????<mapping?resource="Course.hbm.xml"?/>??
  33. ????????<mapping?resource="Employee.hbm.xml"?/>??
  34. ??
  35. ????</session-factory>??
  36. ??
  37. </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代码??收藏代码
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <!DOCTYPE?hibernate-reverse-engineering?PUBLIC?"-//Hibernate/Hibernate?Reverse?Engineering?DTD?3.0//EN"?"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd"?>??
  3. ??
  4. <hibernate-reverse-engineering>??
  5. ??<table-filter?match-catalog="UniversityDatabase"?match-name="admin"/>??
  6. ??<table-filter?match-catalog="UniversityDatabase"?match-name="course"/>??
  7. ??<table-filter?match-catalog="UniversityDatabase"?match-name="department"/>??
  8. ??<table-filter?match-catalog="UniversityDatabase"?match-name="employee"/>??
  9. ??<table-filter?match-catalog="UniversityDatabase"?match-name="employeeaddresses"/>??
  10. ??<table-filter?match-catalog="UniversityDatabase"?match-name="employeecertification"/>??
  11. ??<table-filter?match-catalog="UniversityDatabase"?match-name="employeeeducations"/>??
  12. ??<table-filter?match-catalog="UniversityDatabase"?match-name="employeehours"/>??
  13. ??<table-filter?match-catalog="UniversityDatabase"?match-name="employeememo"/>??
  14. ??<table-filter?match-catalog="UniversityDatabase"?match-name="externalcourse"/>??
  15. ??<table-filter?match-catalog="UniversityDatabase"?match-name="internalcourse"/>??
  16. ??<table-filter?match-catalog="UniversityDatabase"?match-name="person"/>??
  17. ??<table-filter?match-catalog="UniversityDatabase"?match-name="project"/>??
  18. ??<table-filter?match-catalog="UniversityDatabase"?match-name="resourceassignment"/>??
  19. ??<table-filter?match-catalog="UniversityDatabase"?match-name="student"/>??
  20. ??<table-filter?match-catalog="UniversityDatabase"?match-name="user"/>??
  21. ??<table-filter?match-catalog="UniversityDatabase"?match-name="usercourses"/>??
  22. </hibernate-reverse-engineering>??
  相关解决方案