• 自动秒收录
  • 软件:1974
  • 资讯:4527|
  • 收录网站:301505|

IT精英团

多对一关联映射

多对一关联映射

作者/景文

多对一关联映射

作者/景文

image.png

知识点:

多对一(Employee - Department)
映射文件
column=”depart_id”与Employee外键对应 默认于Department主键对应
也可以通过property-ref来指定引用那个属性与外键对应
一对多(Department-Employee)
//映射集合

//指定查询根据depart_id去Employee查询




照样看例子:
第一步搭建hibernate环境和先关配置文件件hibernate环境搭建
第二步:编写Employee Department 实体类
Department.java:
public class Department {
private int id;
private String name;
private Setemployees;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SetgetEmployees() {
return employees;
}
public void setEmployees(Setemployees) {
this.employees = employees;
}
}
Employee.java:
public class Employee {
private int id;
private String name;
private Department department;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第三步:编写类的映射文件 分别为:配置文件命名规则见上面博客
Department.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">









Employee.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">







第四步:编写测试类
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
//ManyToOneAdd();
//ManyToOnd
// Employee employee=ManyToOneQuery(1);
// //这里因为对代理对象进行了初始所有可以在这里得到引用对象
// System.out.println("name:"+employee.getDepartment().getName());
//OneToMany
Department department=QueryDepartment(1);
System.out.println("department length:"+department.getEmployees().size());
}
public static void ManyToOneAdd()
{
Session session=null;
Transaction transaction=null;
Department department=new Department();
department.setName("学生部");
Employee employee1=new Employee();
employee1.setName("张三");
employee1.setDepartment(department);
Employee employee=new Employee();
employee.setName("李勇");
employee.setDepartment(department);
try {
session = HibernateUtil.getSession();
transaction=session.beginTransaction();
session.save(department);
session.save(employee);
session.save(employee1);
transaction.commit();
} catch (Exception e) {
if(session!=null)
{
session.close();
}
}
}
//many-to-on
public static Employee QueryEmployee(int id)
{
Session session=null;
Transaction transaction=null;
Employee employee=null;
try {
session = HibernateUtil.getSession();
transaction=session.beginTransaction();
//这里得到是一个代理对象
employee=(Employee)session.get(Employee.class, id);
System.out.println(employee);
String name=employee.getDepartment().getName();
//初始化代理对象
Hibernate.initialize(employee);
//System.out.println("name:"+name);
transaction.commit();
} catch (Exception e) {
if(session!=null)
{
session.close();
}
return null;
}
return employee;
}
//one-to-many
public static Department QueryDepartment(int id)
{
Session session=null;
Transaction transaction=null;
Department department=null;
try {
session = HibernateUtil.getSession();
transaction=session.beginTransaction();
//下面这种查询只能通过Id查询
department=(Department)session.get(Department.class, id);
Hibernate.initialize(department);
transaction.commit();
} catch (Exception e) {
if(session!=null)
{
session.close();
}
return null;
}
return department;
}
}
第五步:测试....
源码见附件:

点击这里复制本文地址 以上内容由IT精英团整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
退出阅读|首页