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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

MyEclipse中hibernate反向工程实例(一对多关联)
1、数据库设计
实例对象为工作人员和部门,关系为一个部门对应多个工作人员,一个工作人员只属于一个部门
见表语句如下:(先用Navicat建的表,然后导出的sql语句),数据库名称为test SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS`t_department`;
CREATE TABLE`t_department`(
`id`int(11)NOT NULL auto_increment,
`departmentname`varchar(40)NOT NULL,
`departmentlocation`varchar(100)NOT NULL,
PRIMARY KEY(`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS`t_user`;
CREATE TABLE`t_user`(
`id`int(11)NOT NULL auto_increment,
`username`varchar(20)NOT NULL,
`password`varchar(20)NOT NULL,
`departmentid`int(11)NOT NULL,
PRIMARY KEY(`id`),
KEY`departmentid`(`departmentid`),
CONSTRAINT`departmentid`FOREIGN KEY(`departmentid`)REFERENCES `t_department`(`id`)
)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 Hibernate Capabilities…如下图所示:
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.java
package com.lqh.beans;
/**
*User entity.@author MyEclipse Persistence Tools */
public class User implements java.io.Serializable{
//Fields
private Integer id;
private Department department;
private String username;
private String password;
//Constructors
/**default constructor*/
public User(){
}
/**full constructor*/
public User(Department department,String username,String password){
this.department=department;
ername=username;
this.password=password;
}
//Property accessors
public Integer getId(){
return this.id;
}
public void setId(Integer id){
this.id=id;
}
public Department getDepartment(){
return this.department;
}
public void setDepartment(Department department){ this.department=department;
}
public String getUsername(){
return ername;
}
public void setUsername(String username){
ername=username;
}
public String getPassword(){
return this.password;
}
public void setPassword(String password){ this.password=password;
}
}
Department.java
package com.lqh.beans;
import java.util.HashSet;
import java.util.Set;
/**
*Department entity.@author MyEclipse Persistence Tools
*/
public class Department implements java.io.Serializable{
//Fields
private Integer id;
private String departmentname;
private String departmentlocation;
private Set users=new HashSet(0);
//Constructors
/**default constructor*/
public Department(){
}
/**minimal constructor*/
public Department(String departmentname,String departmentlocation){ this.departmentname=departmentname;
this.departmentlocation=departmentlocation;
}
/**full constructor*/
public Department(String departmentname,String departmentlocation,
Set users){
this.departmentname=departmentname;
this.departmentlocation=departmentlocation;
ers=users;
}
//Property accessors
public Integer getId(){
return this.id;
}
public void setId(Integer id){
this.id=id;
}
public String getDepartmentname(){
return this.departmentname;
}
public void setDepartmentname(String departmentname){ this.departmentname=departmentname;
}
public String getDepartmentlocation(){
return this.departmentlocation;
}
public void setDepartmentlocation(String departmentlocation){ this.departmentlocation=departmentlocation;
}
public Set getUsers(){
return ers;
}
public void setUsers(Set users){
ers=users;
}
}
编写测试类代码,如下:
package com.lqh.test;
import org.hibernate.Session;
import org.junit.Test;
import com.lqh.beans.Department;
import er;
import com.lqh.utils.HibernateSessionFactory;
public class HibernateTest{
/**
*测试保存部门
*/
@Test
public void testSaveDepartment(){
Department department=new Department();
department.setDepartmentlocation("清河小营东路");
department.setDepartmentname("北京信息科技");
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(department);
session.getTransaction().commit();
session.close();
}
/**
*测试保存用户,该用户已存在对应Department
*/
@Test
public void testSavaUser(){
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
Department department=(Department)
session.load(Department.class,1);
User user=new User();
user.setUsername("hehe");
user.setPassword("123455");
user.setDepartment(department);
session.save(user);
session.getTransaction().commit();
session.close();
}
/**
*测试保存用户,该用户对应的Department未存在数据库中
*此处注意,要将User.hbm.xml中的cascade设为all
*/
@Test
public void testSaveUserAndDepartment(){
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
Department department=new Department();
department.setDepartmentname("中央人民政府");
department.setDepartmentlocation("北京");
User user=new User();
user.setUsername("UserAndDepartment");
user.setPassword("123455");
user.setDepartment(department);
session.save(user);
session.getTransaction().commit();
session.close();
}
}。

相关文档
最新文档