- 浏览: 830770 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (379)
- struts (5)
- hibernate (16)
- spring (16)
- ssh (20)
- MySQL (16)
- 数据库脚本 (2)
- DownLoad (1)
- GAE (5)
- Java (103)
- LoadRunner (2)
- VF (1)
- 学习资料 (24)
- 软件使用 (21)
- 通信类 (4)
- 生活 (3)
- J2ME (1)
- 心理学 (1)
- Linux (26)
- Android (3)
- Oracle (1)
- 面向对象概念&面试准备 (11)
- ExtJs (2)
- Google Map (1)
- Flex (47)
- 算法研究 (1)
- share (20)
- python (1)
- MongoDB (7)
- centos6 (13)
- C++ (8)
- DB2 (3)
- C# (1)
- 代码片段 (24)
- Lucene (2)
- php (1)
- NodeJS (1)
- Express (1)
最新评论
-
shua1991:
已阅,我表示同意。
Eclipse统计代码行数 -
nakedou:
写的不错,挺详细的
在CentOS中使用 yum 安装MongoDB及服务器端配置 -
sjp524617477:
好方法
Eclipse统计代码行数 -
simpletrc:
<script>ale ...
Java写到.txt文件,如何实现换行 -
csdn_zuoqiang:
Apache Ftp Server,目前是1.0.4,非常好的 ...
Apache FtpServer在64位系统下服务不能启动解决方法
多对一关联关系:
模型:员工Employee — 部门Department
package Domain; public class Employee { 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; } private int id; private String name; private Department depart; }
package Domain; import java.util.Set; public class 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 Set<Employee> getEmps() { return emps; } public void setEmps(Set<Employee> emps) { this.emps = emps; } private int id; private String name; private Set<Employee> emps ; }
<?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="Domain"> <class name="Employee" table="employee"> <id name="id"> <generator class="native" /> </id> <property name="name" unique="true"/> <many-to-one name="depart" column="depart_id"/> </class> </hibernate-mapping>
<?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="Domain"> <class name="Department" table="department"> <id name="id"> <generator class="native" /> </id> <property name="name" unique="true"/> <set name="emps"> <key column="depart_id"/> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
package Dao; import Domain.Employee; public interface EmployeeDAO { public void saveEmployee(Employee emp); public Employee findEmployeeByName(String name); public Employee findEmployeeById(int id); public void updateEmployee(Employee emp); public void removeEmployee(Employee emp); }
package Dao; import Domain.Department; public interface DepartmentDAO { public void saveDepartment(Department depart); public Department findDepartmentByName(String name); public Department findDepartmentById(int id); public void updateDepartment(Department depart); public void removeDepartment(Department depart); }
package Dao.Impl; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import Utils.hibernateUtil; import Dao.EmployeeDAO; import Domain.Employee; public class EmployeeDAOImpl implements EmployeeDAO { // 保存员工 public void saveEmployee(Employee emp) { Session s = null; Transaction tx = null; try{ s = hibernateUtil.getSession(); tx = s.beginTransaction(); s.save(emp); tx.commit(); }catch (HibernateException e) { if(tx != null){ tx.rollback(); } throw e; }finally{ if(s != null){ s.close(); } } } // 根据姓名查询员工 public Employee findEmployeeByName(String name) { Session s = null ; try{ s = hibernateUtil.getSession(); String hql = "from Employee as emp where emp.name=:name"; Query query = s.createQuery(hql); query.setString("name", name); Employee emp = (Employee) query.uniqueResult(); return emp; }finally{ if(s != null){ s.close(); } } } // 根据员工id查询员工 public Employee findEmployeeById(int id) { Session s = null ; try{ s = hibernateUtil.getSession(); Employee emp = (Employee) s.get(Employee.class, id); return emp; }finally{ if(s != null) { s.close(); } } } // 更新员工信息 public void updateEmployee(Employee emp) { Session s = null; Transaction tx = null; try{ s = hibernateUtil.getSession(); tx = s.beginTransaction(); s.update(emp); tx.commit(); }catch (HibernateException e) { if(tx != null){ tx.rollback(); } throw e; }finally{ if(s != null){ s.close(); } } } // 删除员工 public void removeEmployee(Employee emp) { Session s = null; Transaction tx = null; try{ s = hibernateUtil.getSession(); tx = s.beginTransaction(); s.delete(emp); tx.commit(); }catch (HibernateException e) { if(tx != null){ tx.rollback(); } throw e; }finally{ if(s != null){ s.close(); } } } }
package Dao.Impl; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import Dao.DepartmentDAO; import Domain.Department; import Utils.hibernateUtil; public class DepartmentDAOImpl implements DepartmentDAO { // 保存部门 public void saveDepartment(Department depart) { Session s = null; Transaction tx = null; try{ s = hibernateUtil.getSession(); tx = s.beginTransaction(); s.save(depart); tx.commit(); }catch (HibernateException e) { if(tx != null){ tx.rollback(); } throw e; }finally{ if(s != null){ s.close(); } } } // 根据name查找部门 public Department findDepartmentByName(String name) { Session s = null ; try{ s = hibernateUtil.getSession(); String hql = "from Department as depart where depart.name=:name"; Query query = s.createQuery(hql); query.setString("name", name); Department depart = (Department) query.uniqueResult(); return depart; }finally{ if(s != null){ s.close(); } } } // 根据id查找部门 public Department findDepartmentById(int id) { Session s = null ; try{ s = hibernateUtil.getSession(); Department depart = (Department) s.get(Department.class, id); return depart; }finally{ if(s != null) { s.close(); } } } // 更新部门 public void updateDepartment(Department depart) { Session s = null; Transaction tx = null; try{ s = hibernateUtil.getSession(); tx = s.beginTransaction(); s.update(depart); tx.commit(); }catch (HibernateException e) { if(tx != null){ tx.rollback(); } throw e; }finally{ if(s != null){ s.close(); } } } // 删除部门 public void removeDepartment(Department depart) { Session s = null; Transaction tx = null; try{ s = hibernateUtil.getSession(); tx = s.beginTransaction(); s.delete(depart); tx.commit(); }catch (HibernateException e) { if(tx != null){ tx.rollback(); } throw e; }finally{ if(s != null){ s.close(); } } } }
package Dao.Test; import org.hibernate.Session; import org.hibernate.Transaction; import Utils.hibernateUtil; import Domain.Department; import Domain.Employee; public class Many2OneTest { public static void main(String[] args) { add(); } public static Department add(){ Session s = null ; Transaction tx = null; try{ Department depart = new Department(); depart.setName("xuan chuan bu"); Employee emp = new Employee(); emp.setDepart(depart);// 对象模型,建立两个对象间的关联关系 emp.setName("zhang zuoqiang"); s = hibernateUtil.getSession(); tx = s.beginTransaction(); s.save(depart); s.save(emp); tx.commit(); return depart; }finally{ if(s != null){ s.close(); } } } }
评论
1 楼
csdn_zuoqiang
2010-08-03
一对多测试:
package Dao.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import Utils.hibernateUtil;
import Domain.Department;
import Domain.Employee;
public class One2ManyTest {
public static void main(String[] args) {
Department depart = add();
queryDepart(depart.getId());
}
static Department queryDepart(int departId) {
Session s = null;
try {
s = hibernateUtil.getSession();
Department depart = (Department) s.get(Department.class, departId);
System.out.println("emp size: " + depart.getEmps().size());
return depart;
} finally {
if (s != null)
s.close();
}
}
static Department add() {
Session s = null;
Transaction tx = null;
try {
Department depart = new Department();
depart.setName("+Manager+");
Employee emp1 = new Employee();
emp1.setDepart(depart);
// 对象模型:建立两个对象的关联
emp1.setName("emp name1");
Employee emp2 = new Employee();
emp2.setDepart(depart);
// 对象模型:建立两个对象的关联
emp2.setName("emp name2");
/*Set<Employee> emps = new HashSet<Employee>();
emps.add(emp1);
emps.add(emp2);
depart.setEmps(emps);*/
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.save(depart);
s.save(emp1);
s.save(emp2);
System.out.println("-----------------");
tx.commit();
return depart;
} finally {
if (s != null)
s.close();
}
}
}
package Dao.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import Utils.hibernateUtil;
import Domain.Department;
import Domain.Employee;
public class One2ManyTest {
public static void main(String[] args) {
Department depart = add();
queryDepart(depart.getId());
}
static Department queryDepart(int departId) {
Session s = null;
try {
s = hibernateUtil.getSession();
Department depart = (Department) s.get(Department.class, departId);
System.out.println("emp size: " + depart.getEmps().size());
return depart;
} finally {
if (s != null)
s.close();
}
}
static Department add() {
Session s = null;
Transaction tx = null;
try {
Department depart = new Department();
depart.setName("+Manager+");
Employee emp1 = new Employee();
emp1.setDepart(depart);
// 对象模型:建立两个对象的关联
emp1.setName("emp name1");
Employee emp2 = new Employee();
emp2.setDepart(depart);
// 对象模型:建立两个对象的关联
emp2.setName("emp name2");
/*Set<Employee> emps = new HashSet<Employee>();
emps.add(emp1);
emps.add(emp2);
depart.setEmps(emps);*/
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.save(depart);
s.save(emp1);
s.save(emp2);
System.out.println("-----------------");
tx.commit();
return depart;
} finally {
if (s != null)
s.close();
}
}
}
发表评论
-
Hibernate配置文件中指定MySQL数据库的编码方式
2010-10-02 21:50 6562Hibernate配置文件中指定MySQL数据库的编码方式 ... -
hibernate主键生成策略设置不当导致的错误
2010-09-05 12:53 8687ERROR - Exception executing bat ... -
ehcache版本问题导致的一个错误
2010-09-05 10:53 8137org.springframework.beans.facto ... -
hibernate如何设置数据库中字段的默认值
2010-09-03 20:30 2066在配置文件中 <property name=&qu ... -
hibernate3 小结
2010-08-05 16:24 9861、Configuration/SessionFactor ... -
hibernate3的缓存管理
2010-08-05 16:01 8741、Cache简介: 缓 ... -
Hibernate 继承关系的映射
2010-08-04 18:38 1006Hibernate 中支持的 3种类型的继承关系: 1, ... -
hibernate inverse,cascade说明
2010-08-04 00:25 11031.cascade="..."? cas ... -
hibernate笔记整理
2010-08-03 16:36 1278原文地址:http://blog.csdn ... -
hibernate关联关系-组件关联
2010-08-03 13:28 914组件关联关系的映射与原理: 模型:User — Nam ... -
hibernate关联关系-一对一
2010-08-03 13:23 841hibernate关联关系:一对一 模型:Person ... -
hibernate CRUD模板
2010-08-01 18:18 2152实验步骤: 1.设计domain对象User。 2.设计U ... -
Hibernate setFirstResult()和setMaxResults()实现分页查询
2010-07-31 16:04 3594Hibernate 可以实现分页查询,例如: 从第2万条开始取 ... -
hibernate-001
2010-07-31 01:05 1034package com.hibernate.domain; ... -
DAO编程模式
2010-06-03 15:32 874J2EE开发人员使用数据访问对象(DAO)设计模式把底层的数据 ...
相关推荐
这里包含了hibernate多对一单向关联关系实现源码,希望对你有用。
Hibernate映射一对多关联关系
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向关联 │ ├─ 一对一连接表单向关联 │ ├─ 一对多外键单向关联 │ ├─ 一对多连接表单向关联 │ ├─ 多对一外键单向关联 │ ...
包含《多对多双向关联映射》《多对一单向关联映射》《多对一双向关联映射》《一对多单向关联映射》等文档,并有图解及例子,非常适合新手学习,尤其是刚刚接触hibernate,对映射关系不清楚的。。。。
hibernate one-to-one 一对一唯一外键关联映射_单向 and 双向
hibernate关联关系多对一实例
hibernate实现多对多关联关系源码
NULL 博文链接:https://dreamzhong.iteye.com/blog/1200915
Hibernate关联关系(一对多)代码的具体实现,包括保存班级;保存学生;保存班级的时候同时保存学生;已经存在一个班级,新建一个学生,并且建立该学生和该班级之间的关系等一对多关系的情况。不足之处还望谅解
hibernate关联关系
Hibernate关联关系总结 一对多 多对一 多对多
NULL 博文链接:https://dreamzhong.iteye.com/blog/1201615
详细讲解hibernate的关联关系,以及用实例讲解,非常详细
传智播客李勇hibernate源码1-20课,目录如下:01_hibernate介绍与动手入门体验;02_hibernate入门案例的细节分析; 03_hibernate入门案例的代码优化; 04_Session接口及get|load|persist方法 05_实体对象的三种状态...
Hibernate 关联关系映射分类
Hibernate实体关联关系映射--学习总结.pdf
Hibernate一对一,一对多,多对多实例
Hibernate 一对多关联映射的测试,主要使用部门与员工的级联关系进行添加测试.
免积分的资源,希望可以帮到大家,这是我学习hibernate时候,自己写的代码。希望大家学业有成。