- 浏览: 747010 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
lengzl:
请问,那个Node 是哪个包里面的类?
JAVA 二叉树的递归和非递归遍历 -
gongchuangsu:
总结的很好,感谢感谢
JAVA 二叉树的递归和非递归遍历 -
Caelebs:
666666666 居然是10年发的,难怪截屏自动保存的名字是 ...
截图工具 -
jijiqw:
是注解不是注释。。。
Spring @Transactional (一) -
letueo:
[b][b][b][b][b][b][b][b][b][b][ ...
Spring @Transactional (一)
一,持久化对象:
1,置于session管理下的对象叫做持久化状态
2,新建对象叫做临时状态
3,被session释放的对象叫游离状态
例子:
package basic;
import java.util.Iterator;
import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; * hibernate基本操作
* @author chengzh */
public class Test { private static SessionFactory sessionFactory = null;
static{ Configuration config = new Configuration();
//利用配置类对象类构造一个数据存储源,指定配置文件来由buildSessionFactory方法
//构造sessionFactory,必须告诉具体配置文件路径.
sessionFactory = config.configure("basic/hibernate.cfg.xml").buildSessionFactory(); } public void test1() throws Exception{
//通过sessionFactory得到持久化容器session(相当于JDBC里面获得一个连接)
Session session =sessionFactory.openSession(); Transaction tx=null;
try{
//通过session获得一个事务
tx=session.beginTransaction(); Student stu=new Student("zhangshan1",20); session.save(stu);
System.out.println(stu);//持久化状态
stu.setName("wangwu");
// session.delete(stu);//执行delete后,又回到临时状态,只有在持久和游离状态是才能作删除操作
tx.commit();//提交后,对上面操作全部更新
}catch(Exception e){
tx.rollback();
throw e; public void saveStudent(Student student) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(student);
tx.commit();
}catch(Exception e){
tx.rollback();
throw e;
}finally{
session.close(); } public void findAllStudent() throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
//Query查询类也可删除,为HQL语言,对类对象作操作,Student可以加上包名
Query query = session.createQuery("from Student as s order by s.age asc");
List list = query.list();
for(Iterator it = list.iterator();it.hasNext();){
System.out.println(it.next()); tx.commit();
}catch(Exception e){
tx.rollback();
throw e;
}finally{
session.close(); } public void findStudent(String name) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
//由session得到查询器Query.为HQL语言,对类对象作操作,Student可以加上包名
Query query = session.createQuery("from Student as s where s.name=:stuName order by s.age asc");
query.setString("stuName",name);
//由Query的对象来查询数据库,并且将查询上结果以对象的形式返回.存入集合
List list = query.list();
for(Iterator it = list.iterator();it.hasNext();){
System.out.println(it.next()); tx.commit();
}catch(Exception e){
tx.rollback();
throw e;
}finally{
session.close(); } public void loadAndUpdateStudent(Long id) throws Exception{
//根据一个ID号加载一个对象,加载完后会放入session的池中,进入持久化状态
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
//当池中没数据时对数据库操作,根据ID号,把对象加载到session池中,之后 Student student = (Student)session.load(Student.class,id);
//对name进行更新,
//student.setName("new1");
System.out.println(student); //提交到数据库中.使session容器一直处在最新状态.
tx.commit();
session.close(); Session session2 = sessionFactory.openSession();
Transaction tx2 = session2.beginTransaction();
student.setName("lisi");
session2.update(student);//只有从在游离状态时,才能做Update操作.之后下面变成持久状态
tx2.commit();//持久化状态
session2.close();//关闭后又回到游离状态.
}catch(Exception e){
tx.rollback();
throw e;
}finally{
//session.close(); } public void deleteAllStudents() throws Exception {
// 执行delete后,又回到临时状态,只有在持久和游离状态是才能作删除操作
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("delete Student as s");
query.executeUpdate();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback(); throw e;
} finally {
// No matter what, close the session
session.close(); }
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Test test = new Test();
// Student student = new Student("s1",20);
// test.saveStudent(student);
// test.findAllStudent();
// test.findStudent("s1");
test.loadAndUpdateStudent(new Long(2));
// test.deleteAllStudents();
// test.test1();
//关闭sessionFactory
sessionFactory.close(); }
二,五大类型的OID生成方式:
1,sequence :序列化,依赖于数据库,对id号生成有序的数字,xml例子在第一天里面有.
2,increment:由hibernate生成,与数据库无关,每次操作,把最大的ID号自动加1.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"/home/soft01/heguanhua/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="basic.Student" table="student1">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
3,identity:要求数据库支持自动增长字段.
<?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="basic2.Student" table="student1">
<id name="id" column="id" type="long">
<generator class="identity"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
4,assign:表示由程序提供主键的生成.
<?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="basic3.Student" table="STUDENT_na1">
<id name="name" column="name" type="string">
<generator class="assigned"/>
</id>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
5,hilo生成方式:
<?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="basic1.Student" table="student1">
<id name="id" column="id" type="long"> <generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator> <!--
<generator class="increment"/>
-->
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
三,一对多的单向关联
数据库的建立:
CREATE TABLE STUDENT2 ID bigint primary key,
NAME VARCHAR(20),
AGE int
)
CREATE TABLE Course2 ID bigint primary key,
Name VARCHAR(20),
SCORE int,
Student_id bigint not null
);
alter table Course2 add constraint course2_stu2_fk
foreign key(student_id) references student2(id);
hibernate.cfg.xml 和 course.hbm.xml两个文件与双向关联类似.这里不多写.
Student1.hbm.xml文件为:
HYPERLINK "Student1.hbm.xml"HYPERLINK "Student1.hbm.xml"
HYPERLINK "Student1.hbm.xml"<?xml version="1.0"?>
HYPERLINK "Student1.hbm.xml"<!DOCTYPE hibernate-mapping PUBLIC
HYPERLINK "Student1.hbm.xml" "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
HYPERLINK "Student1.hbm.xml" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
HYPERLINK "Student1.hbm.xml"<hibernate-mapping>
HYPERLINK "Student1.hbm.xml" <class name="onetomany.t1.Student" table="student2">
HYPERLINK "Student1.hbm.xml" <id name="id" column="id" type="long">
HYPERLINK "Student1.hbm.xml" <generator class="increment"/>
HYPERLINK "Student1.hbm.xml" </id>
HYPERLINK "Student1.hbm.xml" <property name="name" column="name" type="string"/>
HYPERLINK "Student1.hbm.xml" <property name="age" column="age" type="int"/>
HYPERLINK "Student1.hbm.xml" </class>
HYPERLINK "Student1.hbm.xml"</hibernate-mapping>
Student类与course类缺省,以下是测试类:
package onetomany.t1;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
private static SessionFactory sessionFactory = null;
static {
Configuration config = new Configuration();
sessionFactory = config.configure("onetomany/t1/hibernate.cfg.xml")
.buildSessionFactory();
}
public void saveStudentAndCourse() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = new Student("t1", 20);
session.save(student);
Course course1 = new Course("math", 60,student);
Course course2 = new Course("english", 90,student);
session.save(course1);
session.save(course2);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback(); throw e;
} finally {
session.close(); } public void saveStudentAndCourseCaseCade() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = new Student("t1", 20);
Course course1 = new Course("math", 60,student);
Course course2 = new Course("english", 90,student);
session.save(course1);
session.save(course2);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback(); throw e;
} finally {
session.close(); }
public Student findStudent(Long id) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = (Student)session.load(Student.class,id);
tx.commit();
return student;
} catch (Exception e) {
if (tx != null) {
tx.rollback(); throw e;
} finally {
session.close(); } public List findCourseByStudent(Student student) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("from Course as c where c.student.id="+student.getId());
List courses = query.list();
tx.commit();
return courses;
} catch (Exception e) {
if (tx != null) {
tx.rollback(); throw e;
} finally {
session.close(); }
/**
* @param args
* @throws Exception public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Test test = new Test();
test.saveStudentAndCourse();
// test.saveStudentAndCourseCaseCade();
Student stu = test.findStudent(new Long(505));
List list = test.findCourseByStudent(stu);
for(Iterator it = list.iterator();it.hasNext();){
Course course = (Course)it.next();
System.out.println(course.getCouseName()); sessionFactory.close();
}
四,双向一对多关联:
1,HYPERLINK "http://hibernate.cfg.xml/"hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"/home/soft01/heguanhua/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tarena</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="day2/Student.hbm.xml"/>
<mapping resource="day2/Course.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2,HYPERLINK "http://student.hbm.xml/"student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"/home/soft01/heguanhua/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="day2.Student" table="student1">
<id name="id" column="id" type="long">
<!-- 由increment生成OID 把表中最大的ID号每次自动加一 -->
<generator class="increment"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
<!-- 双向一对多,用集合来映射多个课程 -->
<!-- name="courses" 学生类中集合 课程名 -->
<!-- lazy="false" :加载学生类的时候,是否加载与学生关联的课程对象,
当值为false时是指需要加载 -->
<!-- cascade :级联更新,当删除或存取学生对象时
把对应关联的课程对象也做删除或存取操作 -->
<!-- inverse的意义:当学生的一方的inverse设置为true,能够或避免
重复的update操作来提升程序性能 -->
<set
name="courses"
lazy="false"
cascade="all-delete-orphan"
inverse="true"
>
<!-- 表示指定course的外键是什么 -->
<key column="Student_id" />
<!-- 指定一对多的对象的类型为course -->
<one-to-many class="day2.Course" />
</set>
</class>
</hibernate-mapping>
3,course.hbm.xml文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"/home/soft01/heguanhua/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="day2.Course" table="course">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="couseName" column="name" type="string"/>
<property name="score" column="SCORE" type="int"/>
<!-- 从课程来说是多对一的关系 -->
<!-- 下面的column: 指定外键 -->
<!-- cascade: 级联更新,保存或更新课程时,同时保存或更新与课程有关联的
学生信息 -->
<!-- class: 学生属性所存取的对象存取的类型 -->
<!-- student属性不能为空 -->
<many-to-one
name="student"
column="Student_id"
cascade="save-update"
class="day2.Student"
not-null="true"
/>
</class>
</hibernate-mapping>
Student持久化对象类:
package day2;
import java.util.*;
public class Student { private Long id;
private String name;
private int age;
private Set courses;
public Student(String name, int age, Set courses) {
super();
// TODO Auto-generated constructor stub
this.name = name;
this.age = age;
this.courses = courses;
}
public Student(){ } public int getAge() {
return age; public void setAge(int age) {
this.age = age; public Long getId() {
return id; public void setId(Long id) {
this.id = id; public String getName() {
return name; public void setName(String name) {
this.name = name; @Override
public String toString() {
// TODO Auto-generated method stub
return "id "+id+" name "+name+" age"+age;
}
public Set getCourses() {
return courses;
}
public void setCourses(Set courses) {
this.courses = courses;
} }
5,Course持久化对象类:
package day2;
public class Course {
private Long id;
private String couseName;
private int score; private Student student; public Course(String couseName, int score) {
super();
// TODO Auto-generated constructor stub
this.couseName = couseName;
this.score = score;
}
public Course(){ @Override
public String toString() {
// TODO Auto-generated method stub
return " id "+id+" courseName "+couseName+" score = "+score; public String getCouseName() {
return couseName; public void setCouseName(String couseName) {
this.couseName = couseName; public Long getId() {
return id; public void setId(Long id) {
this.id = id; public int getScore() {
return score; public void setScore(int score) {
this.score = score; public Student getStudent() {
return student; public void setStudent(Student student) {
this.student = student; }
6,测试类:
package day2;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
private static SessionFactory sessionFactory = null;
static {
Configuration config = new Configuration();
sessionFactory = config.configure("day2/hibernate.cfg.xml")
.buildSessionFactory();
} * Student.hbm.xml中将inverse="false",会重复执行多余的SQL语句,因此
* 在映射一多对的双向关联时,应该将inverse设置为true; * @throws Exception public void saveStudentAndCourseCascade() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
//一对多的关系,一个学生有多个课程.用SET来存取.
Student student = new Student("t3",20, new HashSet()); Course course = new Course("math",60);
//设置课程对象,里面的Student属性.
course.setStudent(student); student.getCourses().add(course);
/**提交学生对象到数据库中,查看session池中是否存在有要插入student和
* course对象的相同的值,因为映射文件的Cascade定义了级联操作.把student
* 对象插入时,根据映射文件的定义规则.把与之有关的course对象也相应的加载
* 到数据库和session池中. session.save(student);
tx.commit();
} catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback(); e.printStackTrace();
} finally {
// No matter what, close the session
session.close(); } /**
* 注意cascade="all-delete-orphan" , 1,保存或更新Student时,级联保存或更新所有关联的Course对象,相当于cascade=“save-update”
2,当删除时,相当于"delete"
3,删除不再和Student对象关联的所有Course对象
* @throws Exception public void deleteStudent() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = (Student)session.load(Student.class,new Long(12)); //把学生删除时,与之关联的课程对象也跟着删除.
session.delete(student);
tx.commit();
System.out.println(student);
} catch (Exception e) {
if (tx != null) {
tx.rollback(); e.printStackTrace();
} finally {
session.close(); } public void removeCourseFromStudent() throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student=(Student)session.load(Student.class,new Long(13));
//把课程对象的值都迭代出来,通过学生类中的课程Set来找出所有的课程.
Course course=(Course)student.getCourses().iterator().next(); student.getCourses().remove(course); course.setStudent(null);
tx.commit();
}catch (Exception e) {
if (tx != null) {
tx.rollback(); e.printStackTrace();
} finally {
session.close(); } * @param args
* @throws Exception public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Test test = new Test();
test.saveStudentAndCourseCascade();
// test.deleteStudent();
// test.removeCourseFromStudent();
sessionFactory.close();
}
-
1,置于session管理下的对象叫做持久化状态
2,新建对象叫做临时状态
3,被session释放的对象叫游离状态
例子:
package basic;
import java.util.Iterator;
import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; * hibernate基本操作
* @author chengzh */
public class Test { private static SessionFactory sessionFactory = null;
static{ Configuration config = new Configuration();
//利用配置类对象类构造一个数据存储源,指定配置文件来由buildSessionFactory方法
//构造sessionFactory,必须告诉具体配置文件路径.
sessionFactory = config.configure("basic/hibernate.cfg.xml").buildSessionFactory(); } public void test1() throws Exception{
//通过sessionFactory得到持久化容器session(相当于JDBC里面获得一个连接)
Session session =sessionFactory.openSession(); Transaction tx=null;
try{
//通过session获得一个事务
tx=session.beginTransaction(); Student stu=new Student("zhangshan1",20); session.save(stu);
System.out.println(stu);//持久化状态
stu.setName("wangwu");
// session.delete(stu);//执行delete后,又回到临时状态,只有在持久和游离状态是才能作删除操作
tx.commit();//提交后,对上面操作全部更新
}catch(Exception e){
tx.rollback();
throw e; public void saveStudent(Student student) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(student);
tx.commit();
}catch(Exception e){
tx.rollback();
throw e;
}finally{
session.close(); } public void findAllStudent() throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
//Query查询类也可删除,为HQL语言,对类对象作操作,Student可以加上包名
Query query = session.createQuery("from Student as s order by s.age asc");
List list = query.list();
for(Iterator it = list.iterator();it.hasNext();){
System.out.println(it.next()); tx.commit();
}catch(Exception e){
tx.rollback();
throw e;
}finally{
session.close(); } public void findStudent(String name) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
//由session得到查询器Query.为HQL语言,对类对象作操作,Student可以加上包名
Query query = session.createQuery("from Student as s where s.name=:stuName order by s.age asc");
query.setString("stuName",name);
//由Query的对象来查询数据库,并且将查询上结果以对象的形式返回.存入集合
List list = query.list();
for(Iterator it = list.iterator();it.hasNext();){
System.out.println(it.next()); tx.commit();
}catch(Exception e){
tx.rollback();
throw e;
}finally{
session.close(); } public void loadAndUpdateStudent(Long id) throws Exception{
//根据一个ID号加载一个对象,加载完后会放入session的池中,进入持久化状态
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
//当池中没数据时对数据库操作,根据ID号,把对象加载到session池中,之后 Student student = (Student)session.load(Student.class,id);
//对name进行更新,
//student.setName("new1");
System.out.println(student); //提交到数据库中.使session容器一直处在最新状态.
tx.commit();
session.close(); Session session2 = sessionFactory.openSession();
Transaction tx2 = session2.beginTransaction();
student.setName("lisi");
session2.update(student);//只有从在游离状态时,才能做Update操作.之后下面变成持久状态
tx2.commit();//持久化状态
session2.close();//关闭后又回到游离状态.
}catch(Exception e){
tx.rollback();
throw e;
}finally{
//session.close(); } public void deleteAllStudents() throws Exception {
// 执行delete后,又回到临时状态,只有在持久和游离状态是才能作删除操作
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("delete Student as s");
query.executeUpdate();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback(); throw e;
} finally {
// No matter what, close the session
session.close(); }
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Test test = new Test();
// Student student = new Student("s1",20);
// test.saveStudent(student);
// test.findAllStudent();
// test.findStudent("s1");
test.loadAndUpdateStudent(new Long(2));
// test.deleteAllStudents();
// test.test1();
//关闭sessionFactory
sessionFactory.close(); }
二,五大类型的OID生成方式:
1,sequence :序列化,依赖于数据库,对id号生成有序的数字,xml例子在第一天里面有.
2,increment:由hibernate生成,与数据库无关,每次操作,把最大的ID号自动加1.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"/home/soft01/heguanhua/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="basic.Student" table="student1">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
3,identity:要求数据库支持自动增长字段.
<?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="basic2.Student" table="student1">
<id name="id" column="id" type="long">
<generator class="identity"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
4,assign:表示由程序提供主键的生成.
<?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="basic3.Student" table="STUDENT_na1">
<id name="name" column="name" type="string">
<generator class="assigned"/>
</id>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
5,hilo生成方式:
<?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="basic1.Student" table="student1">
<id name="id" column="id" type="long"> <generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator> <!--
<generator class="increment"/>
-->
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
三,一对多的单向关联
数据库的建立:
CREATE TABLE STUDENT2 ID bigint primary key,
NAME VARCHAR(20),
AGE int
)
CREATE TABLE Course2 ID bigint primary key,
Name VARCHAR(20),
SCORE int,
Student_id bigint not null
);
alter table Course2 add constraint course2_stu2_fk
foreign key(student_id) references student2(id);
hibernate.cfg.xml 和 course.hbm.xml两个文件与双向关联类似.这里不多写.
Student1.hbm.xml文件为:
HYPERLINK "Student1.hbm.xml"HYPERLINK "Student1.hbm.xml"
HYPERLINK "Student1.hbm.xml"<?xml version="1.0"?>
HYPERLINK "Student1.hbm.xml"<!DOCTYPE hibernate-mapping PUBLIC
HYPERLINK "Student1.hbm.xml" "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
HYPERLINK "Student1.hbm.xml" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
HYPERLINK "Student1.hbm.xml"<hibernate-mapping>
HYPERLINK "Student1.hbm.xml" <class name="onetomany.t1.Student" table="student2">
HYPERLINK "Student1.hbm.xml" <id name="id" column="id" type="long">
HYPERLINK "Student1.hbm.xml" <generator class="increment"/>
HYPERLINK "Student1.hbm.xml" </id>
HYPERLINK "Student1.hbm.xml" <property name="name" column="name" type="string"/>
HYPERLINK "Student1.hbm.xml" <property name="age" column="age" type="int"/>
HYPERLINK "Student1.hbm.xml" </class>
HYPERLINK "Student1.hbm.xml"</hibernate-mapping>
Student类与course类缺省,以下是测试类:
package onetomany.t1;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
private static SessionFactory sessionFactory = null;
static {
Configuration config = new Configuration();
sessionFactory = config.configure("onetomany/t1/hibernate.cfg.xml")
.buildSessionFactory();
}
public void saveStudentAndCourse() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = new Student("t1", 20);
session.save(student);
Course course1 = new Course("math", 60,student);
Course course2 = new Course("english", 90,student);
session.save(course1);
session.save(course2);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback(); throw e;
} finally {
session.close(); } public void saveStudentAndCourseCaseCade() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = new Student("t1", 20);
Course course1 = new Course("math", 60,student);
Course course2 = new Course("english", 90,student);
session.save(course1);
session.save(course2);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback(); throw e;
} finally {
session.close(); }
public Student findStudent(Long id) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = (Student)session.load(Student.class,id);
tx.commit();
return student;
} catch (Exception e) {
if (tx != null) {
tx.rollback(); throw e;
} finally {
session.close(); } public List findCourseByStudent(Student student) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("from Course as c where c.student.id="+student.getId());
List courses = query.list();
tx.commit();
return courses;
} catch (Exception e) {
if (tx != null) {
tx.rollback(); throw e;
} finally {
session.close(); }
/**
* @param args
* @throws Exception public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Test test = new Test();
test.saveStudentAndCourse();
// test.saveStudentAndCourseCaseCade();
Student stu = test.findStudent(new Long(505));
List list = test.findCourseByStudent(stu);
for(Iterator it = list.iterator();it.hasNext();){
Course course = (Course)it.next();
System.out.println(course.getCouseName()); sessionFactory.close();
}
四,双向一对多关联:
1,HYPERLINK "http://hibernate.cfg.xml/"hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"/home/soft01/heguanhua/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tarena</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="day2/Student.hbm.xml"/>
<mapping resource="day2/Course.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2,HYPERLINK "http://student.hbm.xml/"student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"/home/soft01/heguanhua/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="day2.Student" table="student1">
<id name="id" column="id" type="long">
<!-- 由increment生成OID 把表中最大的ID号每次自动加一 -->
<generator class="increment"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
<!-- 双向一对多,用集合来映射多个课程 -->
<!-- name="courses" 学生类中集合 课程名 -->
<!-- lazy="false" :加载学生类的时候,是否加载与学生关联的课程对象,
当值为false时是指需要加载 -->
<!-- cascade :级联更新,当删除或存取学生对象时
把对应关联的课程对象也做删除或存取操作 -->
<!-- inverse的意义:当学生的一方的inverse设置为true,能够或避免
重复的update操作来提升程序性能 -->
<set
name="courses"
lazy="false"
cascade="all-delete-orphan"
inverse="true"
>
<!-- 表示指定course的外键是什么 -->
<key column="Student_id" />
<!-- 指定一对多的对象的类型为course -->
<one-to-many class="day2.Course" />
</set>
</class>
</hibernate-mapping>
3,course.hbm.xml文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"/home/soft01/heguanhua/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="day2.Course" table="course">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="couseName" column="name" type="string"/>
<property name="score" column="SCORE" type="int"/>
<!-- 从课程来说是多对一的关系 -->
<!-- 下面的column: 指定外键 -->
<!-- cascade: 级联更新,保存或更新课程时,同时保存或更新与课程有关联的
学生信息 -->
<!-- class: 学生属性所存取的对象存取的类型 -->
<!-- student属性不能为空 -->
<many-to-one
name="student"
column="Student_id"
cascade="save-update"
class="day2.Student"
not-null="true"
/>
</class>
</hibernate-mapping>
Student持久化对象类:
package day2;
import java.util.*;
public class Student { private Long id;
private String name;
private int age;
private Set courses;
public Student(String name, int age, Set courses) {
super();
// TODO Auto-generated constructor stub
this.name = name;
this.age = age;
this.courses = courses;
}
public Student(){ } public int getAge() {
return age; public void setAge(int age) {
this.age = age; public Long getId() {
return id; public void setId(Long id) {
this.id = id; public String getName() {
return name; public void setName(String name) {
this.name = name; @Override
public String toString() {
// TODO Auto-generated method stub
return "id "+id+" name "+name+" age"+age;
}
public Set getCourses() {
return courses;
}
public void setCourses(Set courses) {
this.courses = courses;
} }
5,Course持久化对象类:
package day2;
public class Course {
private Long id;
private String couseName;
private int score; private Student student; public Course(String couseName, int score) {
super();
// TODO Auto-generated constructor stub
this.couseName = couseName;
this.score = score;
}
public Course(){ @Override
public String toString() {
// TODO Auto-generated method stub
return " id "+id+" courseName "+couseName+" score = "+score; public String getCouseName() {
return couseName; public void setCouseName(String couseName) {
this.couseName = couseName; public Long getId() {
return id; public void setId(Long id) {
this.id = id; public int getScore() {
return score; public void setScore(int score) {
this.score = score; public Student getStudent() {
return student; public void setStudent(Student student) {
this.student = student; }
6,测试类:
package day2;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
private static SessionFactory sessionFactory = null;
static {
Configuration config = new Configuration();
sessionFactory = config.configure("day2/hibernate.cfg.xml")
.buildSessionFactory();
} * Student.hbm.xml中将inverse="false",会重复执行多余的SQL语句,因此
* 在映射一多对的双向关联时,应该将inverse设置为true; * @throws Exception public void saveStudentAndCourseCascade() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
//一对多的关系,一个学生有多个课程.用SET来存取.
Student student = new Student("t3",20, new HashSet()); Course course = new Course("math",60);
//设置课程对象,里面的Student属性.
course.setStudent(student); student.getCourses().add(course);
/**提交学生对象到数据库中,查看session池中是否存在有要插入student和
* course对象的相同的值,因为映射文件的Cascade定义了级联操作.把student
* 对象插入时,根据映射文件的定义规则.把与之有关的course对象也相应的加载
* 到数据库和session池中. session.save(student);
tx.commit();
} catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback(); e.printStackTrace();
} finally {
// No matter what, close the session
session.close(); } /**
* 注意cascade="all-delete-orphan" , 1,保存或更新Student时,级联保存或更新所有关联的Course对象,相当于cascade=“save-update”
2,当删除时,相当于"delete"
3,删除不再和Student对象关联的所有Course对象
* @throws Exception public void deleteStudent() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = (Student)session.load(Student.class,new Long(12)); //把学生删除时,与之关联的课程对象也跟着删除.
session.delete(student);
tx.commit();
System.out.println(student);
} catch (Exception e) {
if (tx != null) {
tx.rollback(); e.printStackTrace();
} finally {
session.close(); } public void removeCourseFromStudent() throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student=(Student)session.load(Student.class,new Long(13));
//把课程对象的值都迭代出来,通过学生类中的课程Set来找出所有的课程.
Course course=(Course)student.getCourses().iterator().next(); student.getCourses().remove(course); course.setStudent(null);
tx.commit();
}catch (Exception e) {
if (tx != null) {
tx.rollback(); e.printStackTrace();
} finally {
session.close(); } * @param args
* @throws Exception public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Test test = new Test();
test.saveStudentAndCourseCascade();
// test.deleteStudent();
// test.removeCourseFromStudent();
sessionFactory.close();
}
-
发表评论
-
Struts2 xwork中ActionContext和ServletActionContext介绍
2011-03-21 11:26 1433ActionContext(Action上下文) ... -
Struts,Hibernate,Spring经典面试题收藏
2010-12-06 08:44 1322Struts,Hibernate,Spring经 ... -
struts1.2中ActionForm的理解
2010-12-03 14:43 1720初学struts的人我认为首 ... -
struts-config.xml 详解
2010-12-03 12:04 941弄清楚struts-config.xml中各项元素的作用,对于 ... -
struts global-exceptions用法
2010-12-03 11:56 1566在用struts框架写web程序时,有可能会遇到很多异常,如u ... -
<servlet-mapping>元素及其子元素
2010-12-03 11:04 1397<servlet-mapping>元素 ... -
servlet mapping 规则
2010-12-03 10:43 1429servlet mapping有三种<url-patte ... -
常用log4j配置
2010-12-03 09:48 920常用log4j配置 常用log4j配置,一般可以采用两种方 ... -
Log4j简介
2010-12-03 09:47 799在强调可重用组件开发的今天,除了自己从头到尾开发一个可重用 ... -
log4j配置祥解
2010-12-03 09:46 982第一步:加入log4j-1.2.8.j ... -
数据库连接池DBCP
2010-12-02 14:54 1054概念:数据库连接池负责分配、管理和释放数据库连接,它允许应用程 ... -
Hibernate持久化对象三种状态的区分,以及save,update,saveOrUpdate,merge,persist等的使用
2010-11-12 20:22 1569Hibernate的对象有3种状态,分别为:瞬时态(Trans ... -
hibernate核心类简介
2010-11-12 20:16 1161Hibernate Hibernate是一 ... -
J2EE是什么语言
2010-11-11 12:42 5987****** 版权声明 ******** * 在完整保留 ... -
Hibernate最基础的示例
2010-11-04 15:06 1457有关Spring的知识大部分都已经温习完毕,今天开始转向H ... -
struts+spring+hibernate是怎样的架构?
2010-11-01 17:21 1087struts+spring+hibernate是怎 ... -
快速整合struts+spring+hibernate
2010-11-01 17:17 971说明: 使用平台:Eclipse3.2、MyEclipse5 ... -
Spring整合Hibernate
2010-11-01 15:34 1169Spring整合Hibernate的价值在于Spring为Hi ... -
一些有用的网址
2010-11-01 15:10 1002http://wenku.baidu.com/view/7ab ... -
Spring和Hibernate整合开发
2010-11-01 15:00 1082想的很复杂,用起来就那么回事. 怎么回事呢??? 就是把h ...
相关推荐
Hibernate的核心概念之一就是持久化对象的生命周期,这涉及到对象在数据库中的状态及其与Session的关系。 首先,我们来看一下Hibernate中持久化对象的三种基本状态: 1. **瞬时对象(Transient Objects)**: - ...
**标题:Hibernate 操纵持久化对象** 在Java开发中,Hibernate是一个强大的对象关系映射(ORM)框架,它简化了数据库与Java对象之间的交互。本篇将详细讲解如何使用Hibernate来操纵持久化对象,这对于深入理解...
### 精通Hibernate持久化对象技术 #### Hibernate概述 Hibernate是一种开源的对象关系映射(Object Relational Mapping,ORM)框架,它为Java应用程序提供了一种简单、灵活的方式来与数据库进行交互。通过...
本文将深入探讨Hibernate中的持久化对象生命周期,包括瞬时对象、持久化对象和游离对象的状态转换及其特性。 1. 瞬时对象(Transient Objects) 瞬时对象是指通过`new`操作符创建但尚未与数据库记录关联的Java对象...
4. **HQL(Hibernate Query Language)**:面向对象的查询语言,类似于SQL,但更接近Java的语法,用于检索和操作持久化对象。 5. **第二级缓存**:Hibernate支持查询结果的缓存,可以显著提高系统性能,特别是对于...
Hibernate 操作持久化对象是Java开发中用于简化数据库交互的重要工具。它是一个开源的Java持久化框架,由Gavin King创建,并逐渐发展成为Java世界中主流的持久化解决方案。Hibernate的核心在于提供对象-关系映射(ORM...
《精通Hibernate:Java对象持久化详解》是一本深入解析Hibernate技术的专著,它主要针对Java开发者,旨在帮助他们理解和掌握Java对象持久化的精髓。Hibernate作为一款强大的对象关系映射(ORM)框架,极大地简化了...
在理解Hibernate持久化对象的三个状态之前,首先要明白Hibernate是一个对象关系映射(ORM)框架,它将数据库操作转化为对Java对象的操作。这三个状态决定了对象与数据库之间的一致性及生命周期,分别是: 1. **瞬时...
《精通Hibernate:Java对象持久化技术详解》这本书深入剖析了Hibernate这一流行的Java对象关系映射(ORM)框架,旨在帮助开发者全面理解并熟练掌握Hibernate的使用。Hibernate是Java开发中的重要工具,它简化了...
**hibernate持久化技术详解** Hibernate是一款强大的Java对象关系映射(ORM)框架,它为开发者提供了在Java应用中操作数据库的强大工具。通过Hibernate,开发者可以将数据库操作转化为对Java对象的操作,大大降低了...
《精通Hibernate:Java持久化对象技术详解[第二版]》是一部深入探讨Hibernate框架的专业书籍,旨在帮助Java开发者熟练掌握和运用这一强大的ORM(Object-Relational Mapping)工具。Hibernate是Java开发领域中广泛...
【精通Hibernate:Java对象持久化技术】主要涵盖了Java应用程序的分层体系结构、软件模型以及对象持久化的概念,特别是如何使用Hibernate这一ORM框架进行持久化操作。以下是详细的知识点解析: 1. **应用程序分层...
在JDO中,首先需要编写POJO,然后通过一个名为Enhancer的工具在编译后阶段对POJO的字节码进行增强,生成对应的持久化对象。这意味着JDO的转换过程发生在编译期间,而Hibernate的转换是在运行时。 Hibernate的优点...
精通 Hibernate:Java 对象持久化技术详解 第2版 学习Hibernate必备得一本书
精通 Hibernate_Java 对象持久化技术详解(第2版).pdf
《Java精通Hibernate对象持久化技术权威指南》是一本深度探讨Java开发中如何高效地使用Hibernate进行对象持久化的专业书籍。本书旨在帮助Java开发者熟练掌握Hibernate框架,实现数据库操作的简化和自动化,提升软件...
8.3 Java对象在Hibernate持久化层的状态 8.3.1 临时对象的特征 8.3.2 持久化对象的特征 8.3.3 被删除对象的特征 8.3.4 游离对象的特征 8.4 Session接口的详细用法 8.4.1 Session的save()和persist()...
一共有三个分卷。全部下载才能解压。 这本书不错,值得一看。
总结来说,Hibernate的数据持久化机制是通过ORM技术将Java对象与数据库表进行绑定,使得开发者可以通过简单的对象操作实现对数据库的复杂操作。理解并熟练运用Hibernate,不仅可以提高开发效率,还能使应用程序更加...