hibernate 核心开发接口

package com.hibernate.model;

import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;

import com.hibernate.util.HibernateUtil;

public class StudentTest {
 @Test
 public void saveTest() {
  // StudentPK pk=new StudentPK(1,"liuyun");
  Student stu = new Student(true, 3, "MAN");
  // stu.setPk(pk);
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  session.save(stu);
  session.getTransaction().commit();
 }

 @Test
 public void saveWith3StateTest() {
  Student stu = new Student(true, 3, "MAN"); // tansient状态,只有内存中有
  System.out.println("id=" + stu.getId());
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  session.save(stu); //
  System.out.println("id=" + stu.getId());// save之后是一个peisistese的状态//内存中有,数据库中有,缓存中也有
  session.getTransaction().commit();// commit后内存中有,数据库中有detach状态
  System.out.println("id=" + stu.getId());
 }

 @Test
 public void deleteTest() {
  Student stu = new Student(true, 3, "MAN");
  // stu.setPk(pk);
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  session.save(stu);
  session.getTransaction().commit(); // 当commit后之后session已经关闭
  Session session2 = HibernateUtil.getSession();
  session2.beginTransaction();// 可以删除一个detach状态的对象
  session2.delete(stu);
  session2.getTransaction().commit();
 }

 @Test
 public void delete2Test() {
  Student stu = new Student();
  stu.setId(4); // 可以删除一个transient状态的对象,只要这个对象的主键存在即可
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  session.delete(stu);
  session.getTransaction().commit();
 }

 @Test
 public void coreApiTest() {
  Session session = HibernateUtil.getSession();
  session.get(Student.class, 2); // 记住要运用hibernate的coreapi一定要放在一个事物中
 }

 @Test
 public void loadTest() {
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  Student stu = (Student) session.load(Student.class, 2);
  System.out.println("age=" + stu.getAge());
  session.getTransaction().commit();
  // System.out.println("id="+stu.getId());//这是正确的,他可以得到id的值,id已经赋给了dialing对象
  // System.out.println("age="+stu.getAge());//这是错误的,因为他拿到的是一个代理对象

 }

 @Test
 public void getTest() {
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  Student stu = (Student) session.get(Student.class, 2);// 此时拿到的是一个实在的student对象
  session.getTransaction().commit();
  System.out.println("age=" + stu.getAge());
  System.out.println("class=" + stu.getClass());
 }

 @Test
 public void updateTest() {
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  Student stu = (Student) session.get(Student.class, 2);
  session.getTransaction().commit();
  stu.setGender(Gender.MAN); // 此时只会修改man
  Session session2 = HibernateUtil.getSession();
  session2.beginTransaction();
  session2.update(stu); // 可以update一个detach的对象
  session2.getTransaction().commit();
 }

 @Test
 public void update2Test() {
  Student stu = new Student(); // 对于一个transient对象,一定要有个唯一标识才能update
  stu.setGender(Gender.WOMEN);
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  session.update(stu);
  session.getTransaction().commit();
 }

 @Test
 public void update3Test() {
  Student stu = new Student();
  stu.setId(3);
  stu.setGender(Gender.MAN); // 此时会修改gender,会把其他字段修改
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  session.update(stu);
  session.getTransaction().commit();
  System.out.println("age=" + stu.getAge());
 }

 @Test
 public void testUpdate4() {
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  Student stu = (Student) session.get(Student.class, 2);
  stu.setAge(100);
  session.getTransaction().commit();
  System.out.println("gender=" + stu.getGender());
 }

 @Test
 public void update5Test() {
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  Student stu = (Student) session.get(Student.class, 2);
  stu.setAge(300);// 此时会检查如果数据库中与设进去的值相等就不会发sql语句
  session.getTransaction().commit();
  stu.setAge(300);// 此时会发sql语句
  Session session2 = HibernateUtil.getSession();
  session2.beginTransaction();
  session2.update(stu);
  session2.getTransaction().commit();
 }

 @Test
 public void update6Test() {// 一定会发两条查询语句
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  Student stu = (Student) session.get(Student.class, 2);
  stu.setAge(100);
  session.getTransaction().commit();
  stu.setAge(100);
  Session session2 = HibernateUtil.getSession();
  session2.beginTransaction();
  session2.merge(stu);
  session2.getTransaction().commit();
 }

 @Test
 public void update7Test() {
  Session session = HibernateUtil.getSession();//其实hql才是王道啊
  session.beginTransaction();
  Query query = session
    .createQuery("update Student stu set stu.age=200 where stu.id=2");//运行此函数请注意观察发出的sql语句哦
  query.executeUpdate();
  session.getTransaction().commit();
 }

 @Test
 public void update8Test() {
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  Query query = session.createQuery(
    "update Student stu set stu.age=:stuAge where stu.id=:stuId")
    .setInteger("stuAge", 100).setInteger("stuId", 2);//hql语句的语法
  query.executeUpdate();
  session.getTransaction().commit();
 }
 @Test
 public void update9Test() {
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  Query query = session.createQuery(
    "update Student stu set stu.age=? where stu.id=?")
    .setInteger(0, 200).setInteger(1, 2);
  query.executeUpdate();  //记住此处是0开始有别于jdbc哦,请一定要注意哦
  session.getTransaction().commit();
 }
 @Test
 public void testSaveOrUpdate() {
  
  
  Student stu = new Student();
  stu.setAge(300);
  stu.setGender(Gender.MAN);
  
  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  session.saveOrUpdate(stu);  
  session.getTransaction().commit();
  
  stu.setAge(100);
  
  Session session2 = HibernateUtil.getSession();
  session2.beginTransaction();
  session2.saveOrUpdate(stu);
  session2.getTransaction().commit();
//  
 }
 @Test
 public void clearTest(){

  Session session = HibernateUtil.getSession();
  session.beginTransaction();
  Student stu = (Student)session.load(Student.class, 4);
  System.out.println(stu.getGender()); //运行此方法你会惊人的发现他只发出了一条sql语句,这说明了说明呢?其实我们不难看出,他首先会从缓存中拿
  //session.clear();//这是清空缓存
  Student t2 = (Student)session.load(Student.class, 4);
  System.out.println(t2.getGender());
  session.getTransaction().commit();
 }
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭