hibernate 一对多 多对一 关系表 增删改查大礼包ps二级查也有

今天来到混元气功 这货大概的意思就是你中有我 我中有你 ps 这里就要说到维护关系 ps写这个用了我一下午.......也是刚刚好复习到这里 顺便就写写

    注意:一般都在多方维护关系,至于是用单向还是用多向要看业务需求。

    单向 n-1 关联只需从 n 的一端可以访问 1 的一端。

  建立一对多关系关系的表的原则是将一的一方的主键加入到多的一方的表作为外键。这里以员工和部门为例子来演示。以前不用hibernate时建立pojo类要在员工类Emp中加入一个属性,即部门编号deptid.使用hibernate则不同了,需要在“一”的一方类中加入一个set集合,里面存放“多”的一方的对象。而在“多”的一方的类中需要加入一个“一”方的对象。也就是说在Dept类中需要加入一个set集合,存放Emp对象,因为一个部门里面对应多个员工,所以用一个集合来表示。而每一个员工只能属于一个部门,所以员工类Emp里面需要加入一个Depe类对象,表示所属部门。部门类和员工类的代码如下

    

package com.java.bean;

import com.java.bean.Department;

/*员工*/
public class Employee {
private int id;
private String name;
private 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;
} public Department getDepartment() {
return department;
} public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() { return name;
}
}

然后是它的配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.java.bean">
<class name="Employee" table="t_employee">
<id name="id" column="id" type="int">
<generator class="native"></generator>
</id>
<property name="name" column="name" type="string" length="40"></property>
<many-to-one name="department" class="Department" column="department_id" ></many-to-one> </class> </hibernate-mapping>

many-to-one属性:     多一对一

* name:设定待映射的持久化类的名字。

* column:设定和持久化类的属性对应的表的外键。

* class:设定持久化类的属性的类型。

* not-null:是否允许为空。

然后是我们的  Department

package com.java.bean;

import java.util.Set;

public class
Department {
private int id;
private String name;
private Set<Employee> employees; 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 Set<Employee> getEmployees() {
return employees;
} public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
@Override
public String toString() { return name;
}
}

他的配置文件就比较重要 了 要开启维护

  

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.java.bean">
<class name="Department" table="t_department">
<id name="id" column="id" type="int">
<generator class="native"></generator>
</id>
<property name="name" column="name" type="string" length="40"></property>
<set name="employees" inverse="true" lazy="extra" >
<key column="department_id"></key>
<one-to-many class="Employee" />
</set>
</class>
</hibernate-mapping>

innerse 的属性是true则为开启  然后我们要写一个dao类 也就是增删改查大礼包

  

package cn.java.dao;

import java.util.Iterator;
import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import cn.java.domain.Employee;
import cn.java.domain.User; public class EmployeeDao {
private static SessionFactory sf;
static {
Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
sf = cfg.buildSessionFactory();// 获得会话工厂
} // 增加
public void add(Employee emp) {
// 读配置文件,会
Session s = sf.openSession();
Transaction t = null;
try {
t = s.beginTransaction();
s.save(emp);
t.commit();
} catch (Exception e) {
t.rollback();
throw new RuntimeException(e);
} finally {
s.close();
} }
//查询
public Employee findEmployeeById(int id){
Session s = sf.openSession();
Transaction t = null;
Employee em = null;
try {
t = s.beginTransaction();
em = (Employee) s.get(Employee.class, id);
//em = s.createQuery("FROM t_employee e where e.id=1").list();
t.commit();
} catch (Exception e) {
t.rollback();
throw new RuntimeException(e);
} finally {
s.close();
}
return em;
}
//查询所有
public List<Employee> findEmployee(){
Session s = sf.openSession();
Transaction t = null;
List<Employee> list = null;
try {
t = s.beginTransaction();
//em = (Employee) s.get(Employee.class, id);
list = s.createQuery("FROM Employee e where e.id=1")//
.setCacheable(true)//
.list();
t.commit();
} catch (Exception e) {
t.rollback();
throw new RuntimeException(e);
} finally {
s.close();
}
return list;
} //查询 2级缓存 滑稽
public Iterator<Employee> findEmployeeIt(){
Session s = sf.openSession();
Transaction t = null;
Iterator<Employee> it = null;
try {
t = s.beginTransaction();
//em = (Employee) s.get(Employee.class, id);
it = s.createQuery("FROM Employee e where e.id=1").iterate();
t.commit();
} catch (Exception e) {
t.rollback();
throw new RuntimeException(e);
} finally {
//s.close();
}
return it;
}
//移除关系
public Employee removeRelation(int id){
Session s = sf.openSession();
Transaction t = null;
Employee em = null;
try {
t = s.beginTransaction();
em = (Employee) s.get(Employee.class, id);
em.setDepartment(null);
t.commit();
} catch (Exception e) {
t.rollback();
throw new RuntimeException(e);
} finally {
s.close();
}
return em;
} //删除
public void deleteEmployee(int id){
Session s = sf.openSession();
Transaction t = null;
Employee em = null;
try {
t = s.beginTransaction();
em = (Employee) s.get(Employee.class, id);
s.close();
em.setName("haoren");
t.commit();
} catch (Exception e) {
t.rollback();
throw new RuntimeException(e);
} finally {
s.close();
} }
//更新
public void updateEmployee(int id){
Session s = sf.openSession();
Transaction t = null;
Employee em = null;
try {
t = s.beginTransaction();
em = (Employee) s.get(Employee.class, id);
s.close();
em.setName("haoren");
t.commit();
} catch (Exception e) {
t.rollback();
throw new RuntimeException(e);
} finally {
s.close();
} } }

ps如果觉得我加载文件的 方法过时了可以换成 如下

  

  private static SessionFactory sf;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sf = configuration.buildSessionFactory(serviceRegistry);
}catch (Exception e){
throw e;
}
}

嗨呀如果需要测试了方法的请留言 emmmm屁股坐太久麻木了......

上一篇:CentOS配置本地yum源(使用镜像iso文件)


下一篇:Bootstrap 与 ASP.NET MVC 4 不使用 NuGet Package 笔记