hibernate之6.one2many单向
表结构:
实体类图:
CRUD:
Student:
package com.demo.model; import java.io.UnsupportedEncodingException;
import java.util.Set; /**学生信息
* @author wobendiankun
*2014-10-19 下午08:54:29
*/
public class Student {
private int studentId ;
private String studentName ;
private int age;
private Set<Certificate> certificates ;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
String str="";
if(studentName!=null){
try {
str=new String(studentName.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return "Student [studentId=" + studentId + ", studentName="
+ str + ", age=" + age + "]";
}
public Set<Certificate> getCertificates() {
return certificates;
}
public void setCertificates(Set<Certificate> certificates) {
this.certificates = certificates;
} }配置:
<?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 > <class name="com.demo.model.Student" table="t_student">
<id name="studentId" column="student_id">
<generator class="sequence">
<param name="sequence">SEQ_T_STUDENT</param>
</generator>
</id>
<property name="studentName" column="student_name" />
<property name="age" /> <set name="certificates">
<key column="student_id"></key>
<one-to-many class="com.demo.model.Certificate"/>
</set>
</class>
</hibernate-mapping>说明:
<set name="certificates">
<key column="student_id"></key>
<one-to-many class="com.demo.model.Certificate"/>
</set>
name:Student类的certificates属性名
<key column="student_id"></key> :关联字段名称
<one-to-many class="com.demo.model.Certificate"/> : Set元素的类全名
add:
@Test
public void addTest() {
Certificate certificate1 = new Certificate();
certificate1.setCertificateName("aa");
certificate1.setCertificateNo("3a10001");
Certificate certificate2 = new Certificate();
certificate2.setCertificateName("bb");
certificate2.setCertificateNo("3a10002");
Set<Certificate> set=new HashSet<Certificate>();
set.add(certificate1);
set.add(certificate2); Student student = new Student();
student.setStudentName("钱六");
student.setAge(35);
student.setCertificates(set);
Session session = null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
session.save(student);
session.save(certificate1);
session.save(certificate2);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
HibernateUtil.closeSession(session);
} }
发出的SQL:
Hibernate: insert into t_student (student_name, age, student_id) values (? , ?, ?)
Hibernate: insert into t_certificate (certificate_name, certificate_no, certificate_id) values (? , ? , ?)
Hibernate: insert into t_certificate (certificate_name, certificate_no, certificate_id) values (? , ?, ?)
Hibernate: update t_certificate set student_id=? where certificate_id=? Hibernate: update t_certificate set student_id=? where certificate_id=?从两条update语句中,我们发现此时的关联字段由one(Student)的一方来维护。这个明显不合适
loadTest:
@Test
public void loadTest() {
Session session = null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
Student student =(Student) session.load(Student.class, 23);
System.out.println("姓名:"+student.getStudentName());
System.out.println("数量:"+student.getCertificates().size());
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
HibernateUtil.closeSession(session);
} }
发出的SQL:
Hibernate: select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_0_ from t_student student0_ where student0_.student_id=?
姓名:钱六
Hibernate: select certificat0_.student_id as student4_0_1_, certificat0_.certificate_id as certific1_1_, certificat0_.certificate_id as certific1_1_0_, certificat0_.certificate_name as certific2_1_0_, certificat0_.certificate_no as certific3_1_0_ from t_certificate certificat0_ where certificat0_.student_id=? 数量:2loadLazyExtra:
在set标签中加入属性:lazy="extra"
<? 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 > <class name="com.demo.model.Student" table="t_student">
<id name="studentId" column="student_id">
<generator class="sequence">
<param name="sequence">SEQ_T_STUDENT</param>
</generator>
</id>
<property name="studentName" column="student_name" />
<property name="age" /> <set name="certificates" lazy="extra"><!-- lazy="extra" -->
<key column="student_id"></key>
<one-to-many class="com.demo.model.Certificate"/>
</set>
</class>
</hibernate-mapping>执行代码:
@Test
public void loadLazyExtraTest() {
Session session = null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
Student student =(Student) session.load(Student.class, 23);
System.out.println("姓名:"+student.getStudentName());
System.out.println("数量:"+student.getCertificates().size());
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
HibernateUtil.closeSession(session);
}
}发出的SQL:
Hibernate: select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_0_ from t_student student0_ where student0_.student_id=?
姓名:钱六
Hibernate: select count(certificate_id) from t_certificate where student_id =?
数量:2loadAssociatesTest:
@Test
public void loadAssociatesTest() {
Session session = null;
try {
session = HibernateUtil.openSession();
session.beginTransaction();
Student student =(Student) session.load(Student.class, 23);
System.out.println("姓名:"+student.getStudentName());
for(Certificate c:student.getCertificates()){
System.out.println("编号:"+c.getCertificateNo());
}
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
HibernateUtil.closeSession(session);
}
}发出的SQL:
Hibernate: select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_0_ from t_student student0_ where student0_.student_id=?
姓名:钱六
Hibernate: select certificat0_.student_id as student4_0_1_, certificat0_.certificate_id as certific1_1_, certificat0_.certificate_id as certific1_1_0_, certificat0_.certificate_name as certific2_1_0_, certificat0_.certificate_no as certific3_1_0_ from t_certificate certificat0_ where certificat0_.student_id=?
编号:3a10001
编号:3a10002
第二条查询语句用于查询关联对象,事实上我们能够通过一条sql语句查询关联对象,例如以下:
select * from t_student t1
inner join t_certificate t2 on t2.student_id=t1.student_id
where t1.student_id=23;后面再进行优化
hibernate之6.one2many单向的更多相关文章
- hibernate一对一外键单向关联
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- hibernate一对一主键单向关联
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- Hibernate的关联映射——单向1-N关联
Hibernate的关联映射--单向1-N关联 单向1-N关联的持久化类里需要使用集合属性.因为1的一端需要访问N的一端,而N的一端将以集合(Set)形式表现.从这个意义上来看,1-N(实际上还包括N ...
- Hibernate的关联映射——单向1-1关联
Hibernate的关联映射--单向1-1关联 对于单向的1-1关联关系,需要在持久化类里增加代表关联实体的成员变量,并为该成员变量添加setter方法和getter方法.从持久化类的代码上看,单向1 ...
- Hibernate的关联映射——单向N-1关联
Hibernate的关联映射--单向N-1关联 N-1是非常常见的关联关系,最常见的父子关系也是N-1关联,单向的N-1关联只需从N的一端可以访问1的一端. 为了让两个持久化类能够支持这种关联映射,程 ...
- hibernate它 10.many2many单向
在前文hibernate之5.many2one单向提到多对多关系,表结构设计是基于中间表来实现, 以下以用户与角色(多对多)为例,在Hibernate是怎样操作的 表结构设计: 类图: CRUD; S ...
- hibernate之关于一对一单向,双向关联映射
[hibernate]之关于一对一单向,双向关联映射 首先我们来看,Hibernate官方对于一对一单向关联的解释: 基于外键关联的单向一对一关联和单向多对一关联差点儿是一样的. 唯一的不同就是单向一 ...
- 【SSH进阶之路】Hibernate映射——多对一单向关联映射(四)
[SSH进阶之路]Hibernate基本原理(一) ,小编介绍了Hibernate的基本原理以及它的核心,採用对象化的思维操作关系型数据库. [SSH进阶之路]Hibernate搭建开发环境+简单实例 ...
- hibernate它5.many2one单向
关系数据库表之间的关系: 1 正确 1 1 正确 许多 许多 正确 许多 表间关系设计 基于主键关联 基于外键关联 基于中间表 1 对 1关系实现: 基于主键关联 基于外键关联 基于中间表 1 对 多 ...
随机推荐
- Hibernate 快速上手操作入门
本文主要是从技术操作角度去介绍hibernate,并且简单描述如何快速的上手操作hibernate,用于作为与数据库的连接的持久层. 简单讲述一下hibernate究竟是什么,它有什么作用,在理解它的 ...
- 51nod 1096 距离之和最小【中位数】
1096 距离之和最小 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 X轴上有N个点,求X轴上一点使它到这N个点的距离之和最小,输出这个最小的距离 ...
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分)(线段树单点修改)
[ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14968 Solved: 6079[Submit][Stat ...
- Listener监听器笔记1
1.常用的Web事件监听器接口: 1.ServletContextListener:用于监听Web应用的启动和关闭. 2.ServletContextAttributeListener:用于监听Ser ...
- JsonDataObjects序列和还原
JsonDataObjects序列和还原 JsonDataObjects号称DELPHI最快的JSON库,且支持跨平台. // cxg 2017-9-12// Use JsonDataObjects( ...
- 15 个 Docker 技巧和提示
CLI(Command Line Interface,命令行) 好的 docker ps 输出 将 docker ps 输出通过管道重定向到 less -S,避免折行: docker ps -a | ...
- 全国省会城市路网以及POI 数据
- 【Hadoop】Hadoop MR 自定义排序
1.概念 2.代码示例 FlowSort package com.ares.hadoop.mr.flowsort; import java.io.IOException; import org.apa ...
- ecshop_商品描述远程图片自动本地化插件
解压缩文件,覆盖 ecshop 的 \includes\fckeditor文件夹. 这样在后台添加商品的商品详细描述,编辑器最后一个按钮就是自动下载 远程图片到你的网站空间,这样可防止对方网站图片失效 ...
- Node.js 替换文档内容
server.js代码: var http=require('http'); var fs=require('fs'); var server=http.createServer(function(r ...