一对多(以Department-Employee为例)
JavaBean类
一的一方的类(Department类):
package com.hbsi.domain;
//一的一方,部门
import java.util.Set;
public class Department {
private int id;
private String name;
private Set<Employee> emps;
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this.emps = emps;
}
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类):
package com.hbsi.domain;
//多的一方,员工
public class Employee {
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;
}
}
在一对多中:多的一方的类变成一个普通的Java类,而一的一方的类则添加上了一个Set集合(也就是“private Set<Employee> emps; 并且还要建立它的set和get方法,方便在类的外部使用),这样以便于建立关联。
映射文件:
一的一方的类(Department类)的映射文件:
<hibernate-mapping
package="com.hbsi.domain">
<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<set name="emps">
<key column="depart_id"/>
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping>
多的一方的类(Employee类)的映射文件:
<hibernate-mapping
package="com.hbsi.domain">
<class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
</class>
</hibernate-mapping>
在一对多中:在一的一方类的映射文件中添加了
“<set name="emps">
<key column="depart_id"/>
<one-to-many class="Employee"/>
</set>”
set元素映射一对多关联,
namen指的是集合属性emps;
<key column="depart_id">指定外键;
<one-to-many class="Employee">指定要关联的类
配置文件(hibernate.cfg.xml):
<hibernate-configuration>
<session-factory>
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库连接的URL -->
<property name="connection.url">jdbc:mysql:///demo</property>
<!-- 数据库连接用户名 -->
<property name="connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="connection.password">root</property>
<!-- Hibernate方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- 打印SQL语句-->
<property name="hibernate.show_sql">true</property>
<!-- 映射文件 -->
<mapping resource="com/hbsi/domain/User.hbm.xml"/>
<mapping resource="com/hbsi/domain/Department.hbm.xml"/>
<mapping resource="com/hbsi/domain/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
在配置文件中添加<mapping>标签,将映射文件添加到配置文件中。例:<mapping resource="com/hbsi/domain/Department.hbm.xml"/>
<mapping resource="com/hbsi/domain/Employee.hbm.xml"/>
测试类(Many2One.java类)
package com.hbsi.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.hbsi.domain.Department;
import com.hbsi.domain.Employee;
import com.hbsi.hibernate.utils.HibernateUtil;
public class Many2One {
public static void main(String[] args) {
// TODO Auto-generated method stub
add();
query(1);
}
static Department add(){
Session s=null;
Transaction tx=null;
try{
s= HibernateUtil.getSession();
tx=s.beginTransaction();
//增加
Department dep=new Department();
dep.setName("depatr one");
Employee e1=new Employee();
e1.setName("Tom");
e1.setDepart(dep);//对象模型中:对象建立关联关系
s.save(dep);
s.save(e1);
tx.commit();
return dep;
}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 e = (Employee) s.get(Employee.class, empId);
tx.commit();
return e;
}finally{
if(s!=null){
s.close();
}
}
}
}
表结构:
employee表:
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`depart_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK4722E6AE4926F591` (`depart_id`),
CONSTRAINT `FK4722E6AE4926F591` FOREIGN KEY (`depart_id`) REFERENCES `department` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312
department表:
CREATE TABLE `department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312