八、多对一

多对一

 

 

1.根据上图对象模型创建pojo

2.创建Employee.hbm.xmlDepartment.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.myz.domain">

<class name="Department">

<!-- 主键 -->

<id name="id" type="java.lang.Integer">

<generator class="assigned" />

</id>

 

<property name="name" type="java.lang.String">

<column name="name" length="64" not-null="true" />

</property>

</class>

</hibernate-mapping>

 

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.myz.domain">

<class name="Employee">

<id name="id" type="java.lang.Integer">

<generator class="assigned"></generator>

</id>

 

<property name="name" type="java.lang.String">

<column name="name" length="64" not-null="true"></column>

</property>

 

<!-- 多对一,column表示将来自动生成的表的外键名称,默认与name一样 -->

<many-to-one name="dept" column="dept_id"></many-to-one>

</class>

</hibernate-mapping>

 

3.创建hibernate.cfg.xml(添加hbm2ddl.auto属性让hibernate自动创建数据库)

<?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">

 

<!-- Generated by MyEclipse Hibernate Tools.                   -->

<hibernate-configuration>

 

<session-factory>

<property name="dialect">

org.hibernate.dialect.MySQLDialect

</property>

<property name="connection.url">

jdbc:mysql://localhost:3306/test

</property>

<property name="connection.username">root</property>

<property name="connection.password">123456</property>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="myeclipse.connection.profile">mysql</property>

<property name="show_sql">true</property>

<property name="hbm2ddl.auto">update</property>

 

<mapping resource="com/myz/domain/Department.hbm.xml" />

<mapping resource="com/myz/domain/Employee.hbm.xml" />

 

</session-factory>

</hibernate-configuration>

 

4.结果

创建一个session,由于在加载hibernate.cfg.xml(Configuration().configure())会自动创建表,所以创建一个session时,自然表示加载过了配置文件。

数据库变化:生成了两张表

 

 

 

 

5.测试

ts=session.beginTransaction();

Employee e=new Employee();

e.setId(1);

e.setName("宋江");

Department dept=new Department();

dept.setId(1);

dept.setName("财务部");

e.setDept(dept);

 

//先保存部门,再保存员工,其实先保存员工也是可以的,它会先把你的部门编号置空,然后在保存部门的时候,更新你的员工部门编号

session.save(dept);

session.save(e);

 

ts.commit();

 

此时只有员工指向了部门表,我们获得员工所在部门名称,可以直接 employee.getDept().getName(),而如果我们需要获取所有部门编号为1的员工, 可以

 

 

String hql="from Employee where dept.id=1";

//或者String hql="from Employee where dept_id=1";

List<Employee> list = session.createQuery(hql).list();

for(Employee e:list){

System.out.println(e.getName());

}

 

而hibernate其实还可以反向查询,即在Department中通过一个集合来表示可以对应多个学生对象

 

posted @ 2018-02-07 08:15  菜鸡蔡文姬  阅读(87)  评论(0编辑  收藏  举报