多对一(以Employee-Department为例)
JavaBean类:
一的一方的类(Department类):
package com.hbsi.domain;
import java.util.Set;
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类):
package com.hbsi.domain;
public class Employee {
private int id;
private String name;
private Department depart;
public Department getDepart() {
return depart;
}
public void setDepart(Department depart) {
this.depart = 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;
}
}
在多对一中:一的一方的类只是一个普通的Java类,并没什么特定的添加。而多的一方的类则要添加上一的一方类的对象(也就是“private Department depart;”并且还要建立它的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"/>
</class>
</hibernate-mapping>
多的一方的类(Employee类)的映射文件:
<hibernate-mapping
package="com.hbsi.domain">
<!-- 在class中 name指的是类名 table指的是表名 -->
<class name="Employee" table="employee">
<!-- 在id中 name指的是属性名 column指的是字段名,如果没有此属性则默认字段名和属性名一致 -->
<id name="id" column="id">
<!-- generator元素是用来设置生成器 class属性指的是生成器名 -->
<generator class="native"/>
</id>
<!-- 普通字段的映射,其属性和id的一样 -->
<property name="name" column="name"/>
<many-to-one name="depart" column="depart_id" not-null="true"/>
</class>
</hibernate-mapping>
在多对一中:在多的一方类的映射文件中添加了“<many-to-one name="depart" column="depart_id" not-null="true"/>”,
many-to-one元素映射多对一关联,
name属性:指定关联的属性名;
column属性:指定此关联属性在数据库表中的外键字段名。
not-null属性:指定该字段是否可以为空。
配置文件(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