• Hibernate学习笔记


    1. 配置了<property name="current_session_context_class">thread</property> 后,才可使用sf.getCurrentSession();

    2.基本用法,getCurrentSession()不用Close()   

                 openSession() 需要 Close()

    teacher s = new teacher();
    s.setTitle("title");
    s.setName("wzh");
    
    Configuration cfg = new AnnotationConfiguration();
    SessionFactory sf = cfg.configure().buildSessionFactory();
    
    Session session = sf.openSession();
    session.beginTransaction();
    session.save(s);
    session.getTransaction().commit();
    session.close();
    
    Session session2=sf.getCurrentSession();
    session2.beginTransaction();
    session2.save(s);
    session2.getTransaction().commit();
    
    sf.close();

    3.核心操作

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.cfg.Configuration;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    import com.wzh.hibernate.model.teacher;
    
    public class HibernateCoreAPITest {
    
        private static Configuration cfg;
        private static SessionFactory sf;
    
        @BeforeClass
        public static void BeforeClass() {
            cfg = new AnnotationConfiguration();
            sf = cfg.configure().buildSessionFactory();
        }
    
        @Test
        public void testSaveWith3State() {
            // 三种状态
            teacher s = new teacher();
            s.setTitle("title");
            s.setName("wzh");
            // 保存之前是 Transient 内存中一个对象,没ID,缓存也没有
            Session session = sf.getCurrentSession();
            session.beginTransaction();
            session.save(s);
            // 保存之后是 Persistent 内存中有,缓存中有,数据库有(ID)
            System.out.println(s.getId());
            session.getTransaction().commit();
            // 提交之后是 Detached 内存有,缓存没有,数据库有
            System.out.println(s.getId());
        }
    
        @Test
        public void testDelete() {
            teacher s = new teacher();
            s.setId(1);
            // s.setTitle("title");
            // s.setName("wzh");
            // Session session = sf.getCurrentSession();
            // session.beginTransaction();
            // session.save(s);
            // System.out.println(s.getId());
            // session.getTransaction().commit();
            // System.out.println(s.getId());
    
            Session session2 = sf.getCurrentSession();
            session2.beginTransaction();
            session2.delete(s);
            session2.getTransaction().commit();
        }
    
        @Test
        public void testLoad() {
            Session session = sf.getCurrentSession();
            session.beginTransaction();
            teacher t= (teacher) session.load(teacher.class, 2);
            //load是懒加载,在用对象时才发出sql语句取数据
            //生成的是动态代理对象,该对象是元对象的子类
            System.out.println(t.getClass());
            System.out.println(t.toString());
            session.getTransaction().commit();
            System.out.println(t.getName());
            System.out.println(t.toString());
        }
        
        @Test
        public void testGet() {
            Session session = sf.getCurrentSession();
            session.beginTransaction();
            teacher t = (teacher) session.get(teacher.class, 2);
            //get 是直接发sql语句取数据
            System.out.println(t.getClass());
            System.out.println(t.toString());
            session.getTransaction().commit();
            System.out.println(t.toString());
        }
    
        @AfterClass
        public static void AfterClass() {
            sf.close();
        }
    
        public static void main(String[] args) {
            BeforeClass();
        }
    }
    View Code
  • 相关阅读:
    【LOJ #6397】【THUPC2018】—蛋糕 / Cake(DFS)
    【Atcoder Regular Contest 072F】—Dam(单调队列)
    【Atcoder Regular Contest 072F】—Dam(单调队列)
    多测师讲解自动化测试 _RF封装_(三层模式)高级讲师肖sir
    多测师讲解自动化测试 _RF关键字001_( 中)_高级讲师肖sir
    多测师讲解自动化测试 _RF分配id_高级讲师肖sir
    多测师讲解自动化--rf关键字--断言(下)_高级讲师肖sir
    多测师讲解自动化测试 _RF关键字001_(上)_高级讲师肖sir
    多测师讲解自动化测试 _RF模拟鼠标悬停_高级讲师肖sir
    多测师讲解自动化测试 _RF定位iframe框_高级讲师肖sir
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3482950.html
Copyright © 2020-2023  润新知