/**
* session.clear方法把所有的对象从session中清空
*/
@Test
public void testClear(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = (Person)session.get(Person.class, 1L);
person.setPname("asd");
session.clear();//把session中所有的对象清空
session.update(person);//把对象从脱管状态转化成持久化状态
transaction.commit();
session.close();
}
/**
* session.evict把一个对象从session中清空
*/
@Test
public void testEvict(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = (Person)session.get(Person.class, 1L);
person.setPname("asdsss");
session.evict(person);//脱管状态
session.update(person);//把对象的状态转化成持久化状态
transaction.commit();
session.close();
}
/**
* 一个对象是否是持久化对象是针对某一个session而言的
*/
@Test
public void testSession(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = (Person)session.get(Person.class, 1L); //94
person.setPname("asfd");
transaction.commit();
session.close();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
person.setPname("aaaaaaa");//person对象对于94的session来说是一个临时状态的对象
//session.update(person);
transaction.commit();
session.close();
}
/**
* 当执行transaction.commit的时候,hibernate内部会检查session
* 1、如果一个对象为临时状态的对象,则session不会管.例如15-17行
* 2、如果一个对象是持久化状态的对象,如果有ID值,并且和数据库对应,那么先把该对象与 快照进行对比,如果一致,则什么都不做 ,
如果不一致,则发出update语句
* 3、如果一个对象是持久化状态的对象,如果没有ID值,则会发出save语句
*/
@Test
public void testMuplyOption(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
person.setPname("rrr");
person.setPsex("asdfasdf");
Person person3 = new Person();
person3.setPname("rrr");
person3.setPsex("asdfasdf");
session.save(person);
Person person2 = (Person)session.get(Person.class, 1L);
person2.setPname("afds45");
transaction.commit();
session.close();
}
/**
* 在最后一次提交事务前,已经清楚了session和所有person的关系,
* 即person已经是托管状态,就算再提交事务已经没有任何sql语句输出。
* 对数据库操作的前提是,必须是持久化状态
*/
@Test
public void testMutiplyOption2(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
person.setPname("asdfads");
person.setPsex("er");
session.save(person);
transaction.commit();
session.evict(person);
session.close();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.update(person);
session.clear();
transaction.commit();
session.close();
}
public class OneToManySingleTest extends HiberanteUtils{
@Test
public void testSaveClasses(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Classes classes = new Classes();
classes.setCname("传智上海云一期");
classes.setDescription("很牛");
session.save(classes);
transaction.commit();
session.close();
}
@Test
public void testSaveStudent(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = new Student();
student.setSname("班长");
student.setDescription("老牛:很牛");
session.save(student);
transaction.commit();
session.close();
}
@Test
public void testSaveClasses_Student(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Classes classes = new Classes();
classes.setCname("传智上海云二期:");
classes.setDescription("很牛X");
Student student = new Student();
student.setSname("班长");
student.setDescription("老牛:很牛X");
session.save(student);
session.save(classes);
transaction.commit();
session.close();
}
}