Session usage in hibernate

Source: Internet
Author: User

Session usage in hibernate
The main function of Session is to create, read, and delete mapped object class instances. The instance may exist in the following three States:

Temporary status (transient): No persistence, not associated with any Session
Persistent: only associated with one Session

Detached: it has been persisted but is not associated with any Session.

 

1. The instances obtained through the get () or load () methods are all persistent, and the temporary instances are converted to persistent instances.

2. You can call delete () and close () to remove a persistent instance from the network.

3. You can call update (), 0 saveOrUpdate (), lock (), or replicate () for persistence of an instance in the out-of-management status.

Methods in session in myeclipse:

The load () and get () Methods of the Session:

The get () and load () methods provided by the Session interface in hibernate are used to obtain an object. There are some differences between the usage and query performance:

1) when there is no record corresponding to the OID in the database, the load () method throws the org. hibernate. ObjectNotFoundException exception, and the get () method returns null.
(2) the load method uses the configured load Policy (delayed loading by default), while the get () method ignores the configuration and always uses the loading method immediately.

 

The Session interface provides four overloaded get methods to obtain object objects through "persistent class + primary key", "full class name + primary key", and "lock options.

 

public Object get(Class clazz, Serializable id);public Object get(Class clazz, Serializable id, LockOptions lockOptions);public Object get(String entityName, Serializable id);public Object get(String entityName, Serializable id, LockOptions lockOptions);

 public void testGet() throws Exception {                                                  Session session = sessionFactory.openSession();                                                                                                                             Student student = (Student) session.get(Student.class, 1);                           // Student student = (Student) session.get(com.entity.Student, 1);         System.out.println(student);                                                                                                                                                session.close();                                                                }     
Session save () method:
The save () method of Session converts a temporary object to a persistent object.
(1) Add a temporary object to the Session cache to make it persistent.
(2) Select the identifier generator specified by the ing file to assign a unique ID to the persistent object.
(3) plan to execute an insert statement

 

 

Note: The save () method of the session is used to persist the temporary object. The persistence object or free object should not be passed to the save () method.

If you pass the persistence object to the save () method, the save operation in this step is redundant.
If you pass the free object to the save () method, the ID is generated again and saved again.
.

 

Session update () method:

The update () method of the Session converts an unmanaged object to a persistent object. It completes the following operations:
(1) Add the free object to the Session cache and convert it into a persistent object.
(2) execute an update statement.

 

The saveOrUpdate () method of the Session:

The saveOrUpdate () method of the Session also includes the functions of the save () and update () methods. If the input parameter is a temporary object, the save () method is called; if the input parameter is a free object, the update () method is called.

Delete () method of Session:

The delete () method of Session is used to delete a java object from the database. The delete () method can delete both persistent objects and unmanaged objects. The process is as follows:
(1) If the input parameter is a pipe-dropping object, associate the pipe-dropping object with the Session to make it a persistent object. If the parameter is a persistent object, skip this step.
(2) execute a delete statement.
(3) delete an object from the Session cache and the object enters the deletion state.

 

 

System. out. println (get Session ...); session session = HibernateSessionFactory. currentSession (); System. out. println (start Transaction ...); transaction tx = session. beginTransaction (); Test my_hibernate = null; System. out. println (Iterator query ...); iterator iterator = session. iterate (from Test order by xm); while (iterator. hasNext () {my_hibernate = (Test) iterator. next (); System. out. println (my_hibernate.getXm () ++ my_hibernate.getXb ();} System. out. println (List query ...); list list = session. find (from Test order by xm); for (int I = 0; I <list. size (); I ++) {my_hibernate = (Test) list. get (I); System. out. println (my_hibernate.getXm () ++ my_hibernate.getXb ());}
System. out. println (Query ...); query query = session. createQuery (from Test order by xm); list = query. list (); for (int I = 0; I <list. size (); I ++) {my_hibernate = (Test) list. get (I); System. out. println (my_hibernate.getXm () ++ my_hibernate.getXb ();} iterator = query. iterate (); while (iterator. hasNext () {my_hibernate = (Test) iterator. next (); System. out. println (my_hibernate.getXm () ++ my_hibernate.getXb ();} System. out. println (Criteria query ...); criteria criteria = session. createCriteria (Test. class); criteria. add (Expression. eq (xb, f); list = criteria. list (); for (int I = 0; I <list. size (); I ++) {my_hibernate = (Test) list. get (I); System. out. println (my_hibernate.getXm () ++ my_hibernate.getXb ();} System. out. println (update data ...); my_hibernate = (Test) session. load (Test. class, 121); my_hibernate.setXb (f); System. out. println (submit Transaction ...); tx. commit (); System. out. println (close Session ...); hibernateSessionFactory. closeSession ();


 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.