Session usage in Hibernate

Source: Internet
Author: User
Tags unique id

The main function of the session is to provide the creation, read, and delete operations of the mapped entity class instance. The instance may exist in the following three states:

Temporary state (transient): Never persisted, not associated with any session
Persistent State (persistent): associated with only one session

Off-State (detached): persisted, but not currently associated with any session


1, the instance obtained by the Get () or load () method is persisted, and the temporary instance becomes a persisted instance.

2, the persisted instance can be changed to a de-state by calling Delete () and close ().

3, instances of the off-state can be persisted by calling update (), 0saveOrUpdate (), lock (), or replicate ().

MyEclipse the method in the session:



The load () and get () method of the session:

The Get () and load () methods provided by the session interface in Hibernate are used to obtain an entity object, with some differences in usage and query performance:

1) theload () method throws a Org.hibernate.ObjectNotFoundException exception when there is no record in the database corresponding to the OID, and the Get () method returns null
(2) The Load method takes a configured load policy (default is lazy loading), while the Get () method ignores the configuration and always takes the immediate load method.


The session interface provides 4 overloaded get methods that obtain entity objects through persistent class + primary key and full class name + primary key and lock options.

public object get (class Clazz, Serializable ID);p ublic object Get (class Clazz, Serializable ID, lockoptions lockoptions);p Ublic object Get (String entityname, Serializable ID);p ublic object Get (String entityname, Serializable ID, lockoptions Loc Koptions);

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 ();                                                                }     
Save () method for session:
The Save () method of the session turns a temporary object into a persisted object
(1)adding temporary objects to the session cacheSo that it enters the persistence state.
(2) SelectionThe identifier generator specified by the mapping file, assigning a unique ID to the persisted object。
(3) Planexecutes an INSERT statement


Note: the Save () method of the session is used to persist the temporary object. You should not pass a persisted object or a free object to the Save () method

If you pass a persisted object to the Save () method, the step save operation is superfluous.
If the free object is passed to the Save () method, the ID is regenerated and saved once
.


Update () Method for session:

The update () method of the session turns a de-tube object into a persisted object. It completes the following actions:
(1) The free object is added to the session cache to make it a persistent object.
(2) plan to execute an UPDATE statement.


The Saveorupdate () method of the session:

The Saveorupdate () method of the session also contains the function of the Save () method and the update () method, calling the Save () method if the passed parameter is a temporary object, or calling the update () method if the passed in parameter is a free object.

Delete () method for session :

The delete () method of the session is used to delete a Java object from the database. The delete () method can either delete a persisted object or remove a de-tube object. The process is as follows:
(1) If the passed-in parameter is a de-tube object, the de-tube object is first associated with the session, making it a persisted object. If the argument is a persisted object, the step is ignored.
(2) plan to execute a DELETE statement.
(3) Delete the object from the session cache, and the object goes into the delete state.







system.out.println ("Get Session ..."); Session session = Hibernatesessionfactory.currentsession (); SYSTEM.OUT.PRINTLN ("Beginning 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 queries ..."); 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 = 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 ();




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Session usage in Hibernate

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.