Hibernate uses HQL statements to add, delete, modify, check

All Hibernate operations are done through Session.

The basic steps are as follows:

1: Get the SessionFactory through the configuration file:

  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

  SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");

  //The above configuration is based on the way the spring main configuration file is configured

2: Get a Session through SessionFactory

     Session session=sessionFactory.openSession();

3: Insert, delete, modify and query through session.

   Insert example : (1) Declare a transaction; (2) Session executes save() operation; (3) transaction commits; (4) close Session, optional.                   
             public void insert(Person p){
         Transaction tran=session.beginTransaction( );
      session.save(p); 

                tran.commit();  
        //session.close();             
             }     
    Modified example : (1) Declare a transaction; (2) Session performs update() operation; (3) Transaction commits; (4) Close Session, optional.

             public void update(Person p){

        Transaction tran=session.beginTransaction();

        session.update(p);

           tran.commit();

      // session.close();

      }

    Deletion example (primary key deletion, recommended) : (1) Declare the deleted SQl statement; (2) Create the Query object of the session; (3) Set the parameters of the Query object; (4) Execute the executeUpdate() operation of the Query; (5) )Session transaction commit

     public void delete(int id){

     String hql="delete Person as p where p.id=?";

   //The standard statement is also applicable here: "delete from Person where id=?";

     //The same applies to select and update

     Query query=session.createQuery(hql);

     query.setInteger(0,id);

     query.executeUpdate();

     session.beginTransaction().commit();

    }

   Deletion example (object deletion) : (1) declare a transaction; (2) Session executes delete() operation; (3) transaction commits; (4) closes Session, optional.
    public void delete(Person p){

     Transaction tran = session.beginTransaction();

     //Person p = new Person();

    //p.setid = id; (if only part of the data is passed in)

     session.delete(p);  
     tran.commit();

     session.close();  
   }

   Query example : Query statement does not require transaction commit

(1) Declare the deleted SQl statement; (2) Create the Query object of the session; (3) Set the parameters of the Query object;

   public Persion queryById(int id){

     String hql="from Person as p where p.id=?";

     Query query=session.createQuery();
  
     query.setInteger(0,id);

     List rsList=query.list();

     iterator it=rsList.iterator();

     Person person=null;

     while(it.haseNext()){

       person=(Person)it.next();

     return person;

   }

 

session.delete()- -
session.delete(obj) changes the state of obj to transient. Two cases
1) obj is not in the cache of the session, for example:
         session.delete(new Employee(4));
2) obj exists in the cache of the session, for example:
         Employee employee = (Employee)session.load (Employee.class, new Integer(4));
         session.delete(employee);

Both cases are allowed, and hibernate will send a delete statement to the database.

After delete is executed, if session.load() is called, it can be divided into two cases:
1) Before session.flush(), such as:
           tx.beginTransaction();
    session.delete(new Employee(4));
           session.load(Employee.class, new Integer(4));//occurs before session.flush()
           tx.commit();
      then hibernate will throw ObjectDeletedException: The object with that id was deleted:

2)在session.flush()之后,如:
           tx.beginTransaction();
    session.delete(new Employee(4));
           session.load(Employee.class, new Integer(4));
           tx.commit();

           tx.beginTransaction();
           session.load(Employee.class, new Integer(4));//In the same session, the above tx.commit() flushes the session once.
           tx.commit();
      Then at this time hibernate will only throw ObjectNotFoundException: No row with the give...
indicating that the object cannot be found. If session.get() is used in the second tx, no exception will be thrown.

After delete is executed, if session.save(obj) is called:
           tx.beginTransaction();
           Employee employee = (Employee)session.load(Employee.class, new Integer(4)); 
    session.delete(employee);
           System. out.println(employee);
           session.save(employee);
           System.out.println(employee);
           tx.commit();
      This situation is completely reasonable and legal,
      delete will change employee from persistent to transient status.
      save changes the employee from the transient state to the persistent state.
      When saving a deleted obj, hibernate enforces session.flush() at save, sends a delete statement, and then follows the normal save process. Why this is done, I still don't fully understand.

After delete is executed, if the properties of the obj object are modified, dirtyChecking will not be performed during tx.commit().

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325311752&siteId=291194637