当前位置: 代码迷 >> Eclipse >> MyEclipse中hibernate反向工程范例(一对多关联)
  详细解决方案

MyEclipse中hibernate反向工程范例(一对多关联)

热度:101   发布时间:2016-04-23 01:06:58.0
MyEclipse中hibernate反向工程实例(一对多关联)

1、数据库设计

???????实例对象为工作人员和部门,关系为一个部门对应多个工作人员,一个工作人员只属于一个部门

???????见表语句如下:(先用Navicat建的表,然后导出的sql语句),数据库名称为test

[sql]?view plaincopy
?
  1. SET?FOREIGN_KEY_CHECKS=0;??
  2. ??
  3. DROP?TABLE?IF?EXISTS?`t_department`;??
  4. CREATE?TABLE?`t_department`?(??
  5. ??`id`?int(11)?NOT?NULL?auto_increment,??
  6. ??`departmentname`?varchar(40)?NOT?NULL,??
  7. ??`departmentlocation`?varchar(100)?NOT?NULL,??
  8. ??PRIMARY?KEY??(`id`)??
  9. )?ENGINE=InnoDB?DEFAULT?CHARSET=utf8;??
  10. ??
  11. DROP?TABLE?IF?EXISTS?`t_user`;??
  12. CREATE?TABLE?`t_user`?(??
  13. ??`id`?int(11)?NOT?NULL?auto_increment,??
  14. ??`username`?varchar(20)?NOT?NULL,??
  15. ??`password`?varchar(20)?NOT?NULL,??
  16. ??`departmentid`?int(11)?NOT?NULL,??
  17. ??PRIMARY?KEY??(`id`),??
  18. ??KEY?`departmentid`?(`departmentid`),??
  19. ??CONSTRAINT?`departmentid`?FOREIGN?KEY?(`departmentid`)?REFERENCES?`t_department`?(`id`)??
  20. )?ENGINE=InnoDB?DEFAULT?CHARSET=utf8;??


?

2、使用MyEclipse反向工程生成配置文件和POJO类

???

第一步:配置数据源

1、打开MyEclipse,新建一个web工程,这里命名为hibernate_demo

2、打开数据库设置器:依次单击【window-->Show View-->Other…如下图所示:

3、在弹出的窗口ShowView中选择DB Browser,如下图所示:

4、在DB Browser窗口中,选择显示的图标,单击右键执行新建命令,如下图示

5、弹出Database Driver对话框,在此会要求我们配置数据库的相关信息,具体设置如下图所示,设置完成,单击Finish.

????????????????????

【第二步】引入hibernate配置文件

?

1添加hibernate包:

选中我们的Web工程,依次单击鼠标右键-->MyEclipse-->Add HibernateCapabilities…?如下图所示:

2在弹出的窗口中做如下设置:

Next】,创建hibernate的配置文件

Next】,指明hibernate与数据库的连接

?????

Next】,创建HibernateSessionFactory类,用来获得session。如果前面没有设置包名,要先单击New创建新的包。

?? ?

单击【Finish】按钮

??????????????????????

接下来要给hibernate.cfg.xml文件添加属性:在properties处选择Add…,如下图所示:

单击【Add…】,在Hibernate Properties Wizard页面填入如下图所示信息,最后单击Ok

show_sql:默认为false,如果为true,表示在程序运行时,会在控制台输出SQL语句,这有利于跟中Hibernate的运行状态。在开发和测试阶段,可以将该属性设置为true,以便跟踪、调试程序,在应用发布以后,应将该属性值设置为false,以减少应用的输出信息,提高运行性能。

?

【第三步】添加hibernate映射文件和POJO

1、新建com.lqh.beans

2、在前面设置的数据源上找到我们要操作的表:

DB Browser中选中新建的数据源,单击鼠标右键并选择open connection..

输入数据库的用户名和密码,以创建连接:

找到刚才新建的test数据库,然后是TABLE,如下图所示:

??????????????

生成POJO

???????????

点击Next,配置映射类型(暂时未用到)

????

点击Next,配置反向工程细节

?????????????????????????????????????????

?

?????????? 点击finish即可完成,生成的映射文件以及POJO类如下:

?????????????? User.hbm.xml

???????????????

[html]?view plaincopy
?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <!DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"??
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">??
  4. <!--??
  5. ????Mapping?file?autogenerated?by?MyEclipse?Persistence?Tools?
  6. -->??
  7. <hibernate-mapping>??
  8. ????<class?name="com.lqh.beans.User"?table="t_user"?catalog="test">??
  9. ????????<id?name="id"?type="integer">??
  10. ????????????<column?name="id"?/>??
  11. ????????????<generator?class="native"></generator>??
  12. ????????</id>??
  13. ????????<many-to-one?name="department"?class="com.lqh.beans.Department"?cascade="all"?fetch="select">??
  14. ????????????<column?name="departmentid"?not-null="true"?/>??
  15. ????????</many-to-one>??
  16. ????????<property?name="username"?type="string">??
  17. ????????????<column?name="username"?length="20"?not-null="true"?/>??
  18. ????????</property>??
  19. ????????<property?name="password"?type="string">??
  20. ????????????<column?name="password"?length="20"?not-null="true"?/>??
  21. ????????</property>??
  22. ????</class>??
  23. </hibernate-mapping>??


?

Department.hbm.xml

[html]?view plaincopy
?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <!DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"??
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">??
  4. <!--??
  5. ????Mapping?file?autogenerated?by?MyEclipse?Persistence?Tools?
  6. -->??
  7. <hibernate-mapping>??
  8. ????<class?name="com.lqh.beans.Department"?table="t_department"?catalog="test">??
  9. ????????<id?name="id"?type="integer">??
  10. ????????????<column?name="id"?/>??
  11. ????????????<generator?class="native"></generator>??
  12. ????????</id>??
  13. ????????<property?name="departmentname"?type="string">??
  14. ????????????<column?name="departmentname"?length="40"?not-null="true"?/>??
  15. ????????</property>??
  16. ????????<property?name="departmentlocation"?type="string">??
  17. ????????????<column?name="departmentlocation"?length="100"?not-null="true"?/>??
  18. ????????</property>??
  19. ????????<set?name="users"?inverse="true">??
  20. ????????????<key>??
  21. ????????????????<column?name="departmentid"?not-null="true"?/>??
  22. ????????????</key>??
  23. ????????????<one-to-many?class="com.lqh.beans.User"?/>??
  24. ????????</set>??
  25. ????</class>??
  26. </hibernate-mapping>??


?

User.java

[html]?view plaincopy
?
  1. package?com.lqh.beans;??
  2. ??
  3. /**??
  4. ?*?User?entity.?@author?MyEclipse?Persistence?Tools??
  5. ?*/??
  6. ??
  7. public?class?User?implements?java.io.Serializable?{??
  8. ??
  9. ????//?Fields??
  10. ??
  11. ????private?Integer?id;??
  12. ????private?Department?department;??
  13. ????private?String?username;??
  14. ????private?String?password;??
  15. ??
  16. ????//?Constructors??
  17. ??
  18. ????/**?default?constructor?*/??
  19. ????public?User()?{??
  20. ????}??
  21. ??
  22. ????/**?full?constructor?*/??
  23. ????public?User(Department?department,?String?username,?String?password)?{??
  24. ????????this.department?=?department;??
  25. ????????this.username?=?username;??
  26. ????????this.password?=?password;??
  27. ????}??
  28. ??
  29. ????//?Property?accessors??
  30. ??
  31. ????public?Integer?getId()?{??
  32. ????????return?this.id;??
  33. ????}??
  34. ??
  35. ????public?void?setId(Integer?id)?{??
  36. ????????this.id?=?id;??
  37. ????}??
  38. ??
  39. ????public?Department?getDepartment()?{??
  40. ????????return?this.department;??
  41. ????}??
  42. ??
  43. ????public?void?setDepartment(Department?department)?{??
  44. ????????this.department?=?department;??
  45. ????}??
  46. ??
  47. ????public?String?getUsername()?{??
  48. ????????return?this.username;??
  49. ????}??
  50. ??
  51. ????public?void?setUsername(String?username)?{??
  52. ????????this.username?=?username;??
  53. ????}??
  54. ??
  55. ????public?String?getPassword()?{??
  56. ????????return?this.password;??
  57. ????}??
  58. ??
  59. ????public?void?setPassword(String?password)?{??
  60. ????????this.password?=?password;??
  61. ????}??
  62. ??
  63. }??


Department.java

[html]?view plaincopy
?
  1. package?com.lqh.beans;??
  2. ??
  3. import?java.util.HashSet;??
  4. import?java.util.Set;??
  5. ??
  6. /**??
  7. ?*?Department?entity.?@author?MyEclipse?Persistence?Tools??
  8. ?*/??
  9. ??
  10. public?class?Department?implements?java.io.Serializable?{??
  11. ??
  12. ????//?Fields??
  13. ??
  14. ????private?Integer?id;??
  15. ????private?String?departmentname;??
  16. ????private?String?departmentlocation;??
  17. ????private?Set?users?=?new?HashSet(0);??
  18. ??
  19. ????//?Constructors??
  20. ??
  21. ????/**?default?constructor?*/??
  22. ????public?Department()?{??
  23. ????}??
  24. ??
  25. ????/**?minimal?constructor?*/??
  26. ????public?Department(String?departmentname,?String?departmentlocation)?{??
  27. ????????this.departmentname?=?departmentname;??
  28. ????????this.departmentlocation?=?departmentlocation;??
  29. ????}??
  30. ??
  31. ????/**?full?constructor?*/??
  32. ????public?Department(String?departmentname,?String?departmentlocation,??
  33. ????????????Set?users)?{??
  34. ????????this.departmentname?=?departmentname;??
  35. ????????this.departmentlocation?=?departmentlocation;??
  36. ????????this.users?=?users;??
  37. ????}??
  38. ??
  39. ????//?Property?accessors??
  40. ??
  41. ????public?Integer?getId()?{??
  42. ????????return?this.id;??
  43. ????}??
  44. ??
  45. ????public?void?setId(Integer?id)?{??
  46. ????????this.id?=?id;??
  47. ????}??
  48. ??
  49. ????public?String?getDepartmentname()?{??
  50. ????????return?this.departmentname;??
  51. ????}??
  52. ??
  53. ????public?void?setDepartmentname(String?departmentname)?{??
  54. ????????this.departmentname?=?departmentname;??
  55. ????}??
  56. ??
  57. ????public?String?getDepartmentlocation()?{??
  58. ????????return?this.departmentlocation;??
  59. ????}??
  60. ??
  61. ????public?void?setDepartmentlocation(String?departmentlocation)?{??
  62. ????????this.departmentlocation?=?departmentlocation;??
  63. ????}??
  64. ??
  65. ????public?Set?getUsers()?{??
  66. ????????return?this.users;??
  67. ????}??
  68. ??
  69. ????public?void?setUsers(Set?users)?{??
  70. ????????this.users?=?users;??
  71. ????}??
  72. ??
  73. }??


编写测试类代码,如下:

[html]?view plaincopy
?
  1. package?com.lqh.test;??
  2. ??
  3. import?org.hibernate.Session;??
  4. import?org.junit.Test;??
  5. ??
  6. import?com.lqh.beans.Department;??
  7. import?com.lqh.beans.User;??
  8. import?com.lqh.utils.HibernateSessionFactory;??
  9. ??
  10. public?class?HibernateTest?{??
  11. ????/**??
  12. ?????*?测试保存部门??
  13. ?????*/??
  14. ????@Test??
  15. ????public?void?testSaveDepartment()?{??
  16. ??????????
  17. ????????Department?department?=?new?Department();??
  18. ????????department.setDepartmentlocation("清河小营东路");??
  19. ????????department.setDepartmentname("北京信息科技");??
  20. ??????????
  21. ????????Session?session?=?HibernateSessionFactory.getSession();??
  22. ????????session.beginTransaction();??
  23. ????????session.save(department);??
  24. ????????session.getTransaction().commit();??
  25. ????????session.close();??
  26. ??????????
  27. ????}??
  28. ??????
  29. ????/**??
  30. ?????*?测试保存用户,该用户已存在对应Department??
  31. ?????*/??
  32. ????@Test??
  33. ????public?void?testSavaUser()?{??
  34. ????????Session?session?=?HibernateSessionFactory.getSession();??
  35. ????????session.beginTransaction();??
  36. ??????????
  37. ????????Department?department?=?(Department)?session.load(Department.class,?1);??
  38. ????????User?user?=?new?User();??
  39. ????????user.setUsername("hehe");??
  40. ????????user.setPassword("123455");??
  41. ????????user.setDepartment(department);??
  42. ????????session.save(user);??
  43. ??????????
  44. ????????session.getTransaction().commit();??
  45. ????????session.close();??
  46. ????}??
  47. ??????
  48. ????/**??
  49. ?????*?测试保存用户,该用户对应的Department未存在数据库中??
  50. *??此处注意,要将User.hbm.xml中的cascade设为all??
  51. ?????*/??
  52. ????@Test??
  53. ????public?void?testSaveUserAndDepartment()?{??
  54. ????????Session?session?=?HibernateSessionFactory.getSession();??
  55. ????????session.beginTransaction();??
  56. ??????????
  57. ????????Department?department?=?new?Department();??
  58. ????????department.setDepartmentname("中央人民政府");??
  59. ????????department.setDepartmentlocation("北京");??
  60. ??????????
  61. ????????User?user?=?new?User();??
  62. ????????user.setUsername("UserAndDepartment");??
  63. ????????user.setPassword("123455");??
  64. ????????user.setDepartment(department);??
  65. ????????session.save(user);??
  66. ??????????
  67. ????????session.getTransaction().commit();??
  68. ????????session.close();??
  69. ????}??
  70. ??????
  71. }??

?

此外,还可在POJO类上生成注解,只需在以下两步注意即可:


?

上面这步并不是必须,选在3.2版本以上,默认就支持注解。

?

?

?

其他部分相同即可

  相关解决方案