- 浏览: 17414 次
最新评论
hibernate关联保存(一对多)
2011年06月01日
弄了几天,终于做出Hibernate的第一个关联保存的例子出来了,还跑去广州购书中心看了一个下午的书,
还是感谢网友的帮助,让我弄懂了inverse和cascade这两个属性的使用。
package com.test.ORM;
import java.util.Set;
public class Student {
private int sid ;
private String sname ;
private String sage ;
private Set book;//用来接收、获取Book的对应一端中的
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSage() {
return sage;
}
public void setSage(String sage) {
this.sage = sage;
}
public Set getBook() {
return book;
}
public void setBook(Set book) {
this.book = book;
}
}
//最重点的地方,inverse="false"表示双向关联,也就是关系由两端来控制.cascade="all"就是关联的级别,还有all就是说无论是一方所有的操作(sava、update、delete等)当然也有其他值的。另一方也跟这对应的改变。
package com.test.ORM;
public class Book {
private int bid ;
private String bname ;
private String bprice ;
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getBprice() {
return bprice;
}
public void setBprice(String bprice) {
this.bprice = bprice;
}
}
//用来介绍一方中的students,呵呵,对应Book类中的private Student student;
package com.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.test.ORM.Book;
import com.test.ORM.Student;
public class TestOneToMany {
/**
* @param 测试类
*/
public static void main(String[] args)throws Exception {
SessionFactory sf;
Session session;
Configuration config=new Configuration();
config.configure();
sf=config.buildSessionFactory();
session=sf.openSession();
Transaction ts=session.beginTransaction();
//new 两个对象,只要保存student,book就会自动保存了
Student student=new Student();
student.setSname("student2");
student.setSage("sage2");
Book book=new Book();
book.setBname("bname2");
book.setBprice("bprice2");
book.setStudent(student);
Set bookSet=new HashSet();
bookSet.add(book);
student.setBook(bookSet);
session.save(student);
ts.commit();
session.flush();
session.close();
}
}
要具体了解hibernate 还是去看下hibernate 的保存机制吧,包括OID,事务管理什么的
错误解析:
package com.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.test.ORM.Book;
import com.test.ORM.Student;
public class TestOneToMany {
/**
* @param 测试类
*/
public static void main(String[] args)throws Exception {
SessionFactory sf;
Session session;
Configuration config=new Configuration();
config.configure();
sf=config.buildSessionFactory();
session=sf.openSession();
Transaction ts=session.beginTransaction();
Student student=new Student();
student.setSname("student3");
student.setSage("sage3");
Book book=new Book();
Set bookSet=new HashSet();
for(int i=0;i
发表评论
-
Windows Phone开发(一)-- 开发环境和结构
2012-01-20 01:01 943Windows Phone开发(一)-- 开 ... -
windows mobile activex and atl create (from csdn)
2012-01-20 01:01 791windows mobile activex and atl ... -
Android SDK的初步研究
2012-01-20 01:01 693Android SDK的初步研究 2010年09月27日 ... -
Windows下离线安装Android SDK的简单方法
2012-01-20 01:01 1218Windows下离线安装Android SDK的简单方法 2 ... -
ASP.net/C#编码规范
2012-01-20 01:01 762ASP.net/C#编码规范 2010年06月03日 以 ... -
“嗲妹妹”与”假小子“
2012-01-19 08:50 586“嗲妹妹”与”假小子 ... -
全国各地的美女特点一览
2012-01-19 08:50 1094全国各地的美女特点一览 2011年05月07日 ... -
全国各地的美女特点一览
2012-01-19 08:50 853全国各地的美女特点一 ... -
上海女人之前世今生
2012-01-19 08:50 682上海女人之前世今生 20 ... -
西湖教育给予我的思考
2012-01-19 08:50 665西湖教育给予我的思考 ... -
Windows系统的Regsvr32
2012-01-17 01:18 670Windows系统的Regsvr32 2011 ... -
as3面试题
2012-01-17 01:18 704as3面试题 2011年09月13日 ... -
电脑高手应用技巧荟萃(电脑知识二十六)
2012-01-17 01:18 484电脑高手应用技巧荟萃(电脑知识二十六) 2010年12月07 ... -
regsvr32用法集合
2012-01-17 01:17 813regsvr32用法集合 2011年12月02日 R&a ... -
web-rpg游戏设计简要分析(转)
2012-01-15 20:01 611web-rpg游戏设计简要分析 ... -
as学习使用!!!
2012-01-15 20:00 832as学习使用!!! 2010年05月 ... -
Flash&Flex大全
2012-01-15 20:00 714Flash&Flex大全 2011年02月24日 ... -
2011-12-12
2012-01-15 20:00 4752011-12-12 2011年12月12日 乱码体: -
乡土菜
2012-01-15 20:00 508乡土菜 2011年12月18日 涓 -
Qt初印象
2012-01-11 12:11 559Qt初印象 2011年06月01日 断断续续已经读 ...
相关推荐
有关hibernate中保存多对一关联关系的进一步分析,很详细的介绍了多对一关系
Hibernate关联关系(一对多)代码的具体实现,包括保存班级;保存学生;保存班级的时候同时保存学生;已经存在一个班级,新建一个学生,并且建立该学生和该班级之间的关系等一对多关系的情况。不足之处还望谅解
博文链接:https://llying.iteye.com/blog/220809
NULL 博文链接:https://huihai.iteye.com/blog/901171
通过联接表来建立单向一对多关联不需要描述任何物理映像,表名由以下三个部分组成:主表(ownertable)表名+从表(the other side table)表名,指向主表的外键名:主表表名+下划线+主表主键列名,指向从表的外键名:主...
传智播客李勇hibernate源码1-20课,目录如下:01_hibernate介绍... 17_一对多关联关系的映射与原理分析 18_一对一主键关联关系的映射与原理分析 19_一对一主键关联关系的检索 20_一对一外键关联关系的映射与原理分析
这个项目写的是hibernate一对多的双向关联的保存操作。
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
这种策略支持双向的一对多关联,但不支持 IDENTIFY 生成器策略,因为ID必须在多个表间共享。一旦使用就不能使用AUTO和IDENTIFY生成器。 每个类层次结构一张表 @Entity @Inheritance(strategy=InheritanceType....
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...
7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...
7.2.1 一对多关联 7.2.2 多对多关联 7.2.3 把列添加到联结表 7.2.4 映射map 7.3 多态关联 7.3.1 多态的多对一关联 7.3.2 多态集合 7.3.3 对联合的多态关联 7.3.4 每个具体类一张...
15.3.4 把多对多关联分解为两个一对多关联 15.4 小结 15.5 思考题 第16章 Hibernate的检索策略 16.1 Hibernate的检索策略简介 16.2 类级别的检索策略 16.2.1 立即检索 16.2.2 延迟检索 16.3 一对多...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
本章介绍一对多关联关系的映射方法,重点介绍inverse属性和cascade属性的用法。本章还将介绍通过Hibernate API来保存、修改和删除具有关联关系的对象的方法。 7.1 建立多对一的单向关联关系 148 7.1.1 [many-to-...