• Hibernate里面的几个方法


    1、save方法:

    1 public void testSave() throws Exception {
    2         Session session = HibernateUtils.getInstance().getSession();
    3         session.beginTransaction();
    4         Teacher tac = new Teacher("哄哄",52);
    5         session.save(tac);
    6         session.getTransaction().commit();
    7         session.close();
    8     }

    2、delete方法:

    1 public void testDelete() throws Exception {
    2         Session session = HibernateUtils.getInstance().getSession();
    3         session.beginTransaction();
    4         Teacher tac = new Teacher(2L);
    5         session.delete(tac);
    6         session.getTransaction().commit();
    7         session.close();
    8     }

    3、update方法:

    1 public void testUpdate() throws Exception {
    2         Session session = HibernateUtils.getInstance().getSession();
    3         session.beginTransaction();
    4         Teacher tac = new Teacher(3L,"哄哄",52);
    5         session.update(tac);
    6         session.getTransaction().commit();
    7         session.close();
    8     }

    4、list方法:

    1 public void testList() throws Exception {
    2         Session session = HibernateUtils.getInstance().getSession();
    3         Query query = session.createQuery("select tec from Teacher tec");
    4         @SuppressWarnings("unchecked")
    5         List<Teacher> list = query.list();
    6         System.out.println(list);
    7         session.close();
    8     }

    5、saveorupdate方法:

     1 public void testSaveOrUpdate() throws Exception {
     2         /**
     3          * 利用saveOrUpdate添加对象,要使用的话,要么没有id,要么
     4          * 使用数据库中已有的id
     5          */
     6         Session session = HibernateUtils.getInstance().getSession();
     7         session.beginTransaction();
     8         
     9         /*Teacher tac = new Teacher("哈哈",21);
    10         session.saveOrUpdate(tac);*/
    11         Teacher tac = new Teacher(2l,"小小",21);
    12         session.saveOrUpdate(tac);
    13         
    14         session.getTransaction().commit();
    15         session.close();
    16     }

    6、persist方法:

     1 public void testPersist() throws Exception {
     2         //添加方法JPA规范里面的
     3         Session session = HibernateUtils.getInstance().getSession();
     4         session.beginTransaction();
     5         
     6         Teacher tac = new Teacher("大大",21);
     7         session.persist(tac);
     8         
     9         session.getTransaction().commit();
    10         session.close();
    11     }

    7、merge方法:

     1 public void testMerge() throws Exception {
     2         /**
     3          *    JPA规范里面的一个方法,相当与save
     4          *    1.如果里面没有对象里面没有id,相当于save方法;
     5          *    2.如果有id,但是数据库里面没有,相当于save方法,并且忽略对象里面的id
     6          *    3.如果有id,数据库里面也有,则就相当于update方法
     7          */
     8         Session session = HibernateUtils.getInstance().getSession();
     9         session.beginTransaction();
    10         
    11         //Teacher tac = new Teacher(20L,"啦啦啦",21);
    12         Teacher tac = new Teacher("啦啦啦",21);
    13         session.merge(tac);
    14         
    15         session.getTransaction().commit();
    16         session.close();
    17     }

    8、get方法:

     1 public void testGet() throws Exception {
     2         /**
     3          * 一级缓存:同时执行两个一模一样的操作,只打印一次数据库语句
     4          * Hibernate: select teacher0_.e_id as e1_0_0_,-
     5          * teacher0_.e_name as e2_0_0_, teacher0_.e_age as e3_0_0_ -
     6          * from t_teacher teacher0_ where teacher0_.e_id=?
     7          * Teacher [id=3, name=大大, age=21]
     8          * Teacher [id=3, name=大大, age=21]
     9          */
    10         Session session = HibernateUtils.getInstance().getSession();
    11         session.beginTransaction();
    12         
    13         Teacher tea = (Teacher) session.get(Teacher.class, 3L);
    14         System.out.println(tea);//一次操作
    15         Teacher tea1 = (Teacher) session.get(Teacher.class, 3L);
    16         System.out.println(tea1);//二次操作
    17         
    18         session.getTransaction().commit();
    19         session.close();
    20     }

    9、load方法:

     1 public void testLoad() throws Exception {
     2         Session session = HibernateUtils.getInstance().getSession();
     3         session.beginTransaction();
     4         /**
     5          * session.get在调用这个方法以后立马去数据库查询;
     6          * session.load在调用之后先不管用户的返回值,看下面的对象的非主键getter方法是否调用,
     7          * 如果调用,再去数据库查询,否则一直观望,直到session挂了,在使用load的时候一定要
     8          * 保证id在数据库中有对应的对象
     9          */
    10         /*Teacher tea = (Teacher) session.get(Teacher.class, 4L);
    11         System.out.println("~~~~~~~~~~~~~~~~~~~~");
    12         System.out.println(tea);*/
    13         /**控制台的输出结果:
    14          * Hibernate: select teacher0_.e_id as e1_0_0_, teacher0_.e_name as e2_0_0_, teacher0_.e_age as e3_0_0_ from t_teacher teacher0_ where teacher0_.e_id=?
    15          * ~~~~~~~~~~~~~~~~~~~~
    16          * Teacher [id=4, name=大大, age=21]
    17          */
    18         Teacher tea = (Teacher) session.load(Teacher.class, 4L);
    19         System.out.println("~~~~~~~~~~~~~~~~~~~~~");
    20         System.out.println(tea);
    21         /**控制台的输出结果:
    22          * ~~~~~~~~~~~~~~~~~~~~~
    23          * Hibernate: select teacher0_.e_id as e1_0_0_, teacher0_.e_name as e2_0_0_, teacher0_.e_age as e3_0_0_ from t_teacher teacher0_ where teacher0_.e_id=?
    24          * Teacher [id=4, name=大大, age=21]
    25          */
    26         session.getTransaction().commit();
    27         session.close();
    28     }
  • 相关阅读:
    cocos2D-X 4.0 build
    Boost Download
    史上最全的CSS hack
    web前端js中ES6的规范写法
    爬虫最终杀手锏 --- PhantomJS 详解(附案例)
    移动端300ms延迟的解决方法
    js动态生成html元素并为元素追加属性
    css中border-sizing属性详解和应用
    纯CSS实现Tab切换
    阻止移动端浏览器点击图片会预览的几种方法
  • 原文地址:https://www.cnblogs.com/nice6/p/6550601.html
Copyright © 2020-2023  润新知