您当前的位置:官网首页 > 慢生活 >

Hibernate学习三:Session对象的几个方法

发布时间:2019-12-11 13:19编辑:

    一.前语

      上文Hibernate学习二:Hibernate目标状况及转化中描绘了hibernate中目标的三种状况。天然的,对session中的许多办法,save,update,saveOrUpdate,merge,delete办法,我都有了自己的主意:传什么状况的目标做为这些办法的参数,办法回来给我什么状况的目标。为了验证自己的主意,也为了便利他人,遂做此文。

     

     

    二.你需求知道以下常识

      假如你不愿意参阅上面一篇文章,不要紧,记住下面三句话:

     

    三.public Serializable save throws HibernateException;

     public void test {
     Session session = getSession; 
     Transaction tr = session.beginTransaction; 
     //暂时状况目标 履行以下sql
     //Hibernate: insert into test.user  values 
    // User user = new User;
    // user.setName;
    // user.setPassword;
    // session.save;
     //耐久化状况目标 履行以下sql
     //Hibernate: select user0_.id as id1_0_, user0_.name as name1_0_, user0_.password as password1_0_ from test.user user0_ where user0_.id=?
     //Hibernate: update test.user set name=?, password=? where id=?
    // User user =  session.load;
    // user.setName; //假如user[1]目标曾经的Name便是s6dd,不履行update句子
    // session.save;
     //游离状况目标 履行以下sql
     //Hibernate: select user0_.id as id1_0_, user0_.name as name1_0_, user0_.password as password1_0_ from test.user user0_ where user0_.id=?
     //Hibernate: insert into test.user  values 
     Session session2 = getSession;
     User user =  session2.load;//当经过get或load办法得到的po目标它们都处于persistent,但假如履行delete时,该 po状况就处于detached,
     session2.delete;
     session2.close;
     user.setName;
     session.save;
     tr.commit;
     session.close;
     }

     

     

    四.public void update throws HibernateException;

     

     public void test {
     Session session = getSession; 
     Transaction tr = session.beginTransaction; 
     //暂时状况目标 履行以下sql
     //Hibernate: update test.user set name=?, password=? where id=?
    // User user = new User;
    // user.setName;
    // user.setPassword;
    // session.update;
     //耐久化状况目标 履行以下sql
     //Hibernate: select user0_.id as id1_0_, user0_.name as name1_0_, user0_.password as password1_0_ from test.user user0_ where user0_.id=?
     //Hibernate: update test.user set name=?, password=? where id=?
    // User user =  session.load;
    // user.setName; //假如user[1]目标曾经的Name便是s6dd,不履行update句子
    // session.update;
     //游离状况目标 履行以下sql
     //Hibernate: select user0_.id as id1_0_, user0_.name as name1_0_, user0_.password as password1_0_ from test.user user0_ where user0_.id=?
     //Hibernate: update test.user set name=?, password=? where id=?
     Session session2 = getSession;
     User user =  session2.load;//当经过get或load办法得到的po目标它们都处于persistent,但假如履行delete时,该 po状况就处于detached,
     session2.delete;
     session2.close;
     user.setName;
     session.update;
     tr.commit;
     session.close;
     }

     

    五.public void saveOrUpdate throws HibernateException;

     public void test {
     Session session = getSession; 
     Transaction tr = session.beginTransaction; 
     //暂时状况目标 履行以下sql
     //Hibernate: insert into test.user  values 
    // User user = new User;
    // user.setName;
    // user.setPassword;
    // session.saveOrUpdate;
     //耐久化状况目标 履行以下sql
     //Hibernate: select user0_.id as id1_0_, user0_.name as name1_0_, user0_.password as password1_0_ from test.user user0_ where user0_.id=?
     //Hibernate: update test.user set name=?, password=? where id=?
    // User user =  session.load;
    // user.setName; //假如user[1]目标曾经的Name便是s6dd,不履行update句子
    // session.saveOrUpdate;
     //游离状况目标 履行以下sql
     //Hibernate: select user0_.id as id1_0_, user0_.name as name1_0_, user0_.password as password1_0_ from test.user user0_ where user0_.id=?
     //Hibernate: update test.user set name=?, password=? where id=?
     Session session2 = getSession;
     User user =  session2.load;//当经过get或load办法得到的po目标它们都处于persistent,但假如履行delete时,该 po状况就处于detached,
     session2.delete;
     session2.close;
    // user.setName;
     session.saveOrUpdate;
     tr.commit;
     session.close;
     }

     

    六. public Object merge throws HibernateException;


     

     public void test {
     Session session = getSession; 
     Transaction tr = session.beginTransaction; 
     //暂时状况目标 履行以下sql
     //Hibernate: insert into test.user  values 
    // User user = new User;
    // user.setName;
    // user.setPassword;
    // session.merge;
     //耐久化状况目标 履行以下sql
     //Hibernate: select user0_.id as id1_0_, user0_.name as name1_0_, user0_.password as password1_0_ from test.user user0_ where user0_.id=?
     //Hibernate: update test.user set name=?, password=? where id=?
    // User user =  session.load;
    // user.setName; //假如user[1]目标曾经的Name便是s6dd,不履行update句子
    // session.merge;
     //游离状况目标 履行以下sql
     //Hibernate: select user0_.id as id1_0_, user0_.name as name1_0_, user0_.password as password1_0_ from test.user user0_ where user0_.id=?
     //Hibernate: update test.user set name=?, password=? where id=?
     Session session2 = getSession;
     User user =  session2.load;//当经过get或load办法得到的po目标它们都处于persistent,但假如履行delete时,该 po状况就处于detached,
     session2.delete;
     session2.close;
     user.setName;session.merge;
     tr.commit;
     session.close;
     }

     

     

    七.public void delete throws HibernateException;

     public void test {
     Session session = getSession; 
     Transaction tr = session.beginTransaction; 
     //暂时状况目标 不履行sql
    // User user = new User;
    // user.setName;
    // user.setPassword;
    // session.delete;
     //耐久化状况目标 履行以下sql
     //Hibernate: select user0_.id as id1_0_, user0_.name as name1_0_, user0_.password as password1_0_ from test.user user0_ where user0_.id=?
     //Hibernate: delete from test.user where id=?
    // User user =  session.load;
    // session.delete;
     //游离状况目标 履行以下sql
     //Hibernate: select user0_.id as id1_0_, user0_.name as name1_0_, user0_.password as password1_0_ from test.user user0_ where user0_.id=?
     //Hibernate: delete from test.user where id=?
     Session session2 = getSession;
     User user =  session2.load;//当经过get或load办法得到的po目标它们都处于persistent,但假如履行delete时,该 po状况就处于detached,
     session2.delete;
     session2.close;
     session.delete;
     tr.commit;
     session.close;
     }

     

    七.saveOrUpdate和merge