这里举例子用的是:部门与员工的关系。
一个部门可以对应多个员工,这就是很明显的多对一关联关系。
我们需要建立两个实体类:员工(Employee)、部门(Department)
员工类:Employee.java(在这里边我们增加了一个Department类型的属性,用来做关联)
package cn.itcast.hibernate.domain; public class Employee { private int id; private String name; private Department depart; 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 Department getDepart() { return depart; } public void setDepart(Department depart) { this.depart = depart; } }
部门类Department.java
package cn.itcast.hibernate.domain; public class Department { private int id; private String name; 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; } }
然后,我们要在他们的目录下建立他们的映射文件:
Employee.hbm.xml(在这里我们增加了一个<many-to-one />标签用来规定映射)
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.hibernate.domain"> <class name="Employee" table="tb_Employee"> <id name="id"> <generator class="native"/> </id> <property name="name" /> <many-to-one name="depart" column="depart_id" /> </class> </hibernate-mapping>
Department.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.hibernate.domain"> <class name="Department" table="tb_Department"> <id name="id"> <generator class="native"/> </id> <property name="name" /> </class> </hibernate-mapping>
最后我们需要建立hibernate.cfg.xml,这个文件用来建立与数据库的连接和加载各个实体类的映射文件:
<?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"> <hibernate-configuration> <session-factory> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.url"> jdbc:mysql://localhost:3306/test </property> <property name="connection.username">sa</property> <property name="connection.password">123456</property> <property name="hbm2ddl.auto">create</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="show_sql">true</property> <mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" /> <mapping resource="cn/itcast/hibernate/domain/Department.hbm.xml" /> <mapping resource="cn/itcast/hibernate/domain/Employee.hbm.xml" /> </session-factory> </hibernate-configuration>
到了这里,所有的配置都已经配好了,就要开始测试了。
我们建立一个Many2One.java文件:
package cn.itcast.hibernate; import org.hibernate.Session; import org.hibernate.Transaction; import cn.itcast.hibernate.domain.Department; import cn.itcast.hibernate.domain.Employee; public class Many2One { public static void main(String[] arg){ add(); Employee emp = query(1); } static Department add(){ Session s = null; Transaction tx = null; try{ Department depart = new Department(); depart.setName("depart name"); Employee emp = new Employee(); emp.setDepart(depart); //建立两个对象的关联关系 emp.setName("emp name"); s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(emp); //emp对象和depart对象保存的先后顺序不同也会有一定的差异 s.save(depart); //如果员工(emp)先插入,部门(depart)后插入,会生成三条数据库语句, //因为在两者都插入之后,emp的depart_id字段还未插入,所以还要有最后一条语句插入 tx.commit(); return depart; }finally{ if(s!=null){ s.close(); } } } static Employee query(int empId){ Session s = null; Transaction tx = null; try{ s = HibernateUtil.getSession(); tx = s.beginTransaction(); Employee emp = (Employee)s.get(Employee.class, empId); //根据ID查询 System.out.println(emp.getName()); //查询员工姓名 System.out.println(emp.getDepart().getName()); //根据员工查询属于哪个部门 tx.commit(); return emp; }finally{ if(s!=null){ s.close(); } } } }
多对一的基本操作讲解完毕。
由此可见,Hibernate可以直接用面向对象的语言直接操作关系型数据库。
时间: 2024-10-06 11:50:09