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.核心操作

Hibernate学习笔记
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

 

你可能感兴趣的:(Hibernate)