Hibernate session method, hibernatesession

Source: Internet
Author: User

Hibernate session method, hibernatesession

Import static org. junit. assert. *; import java. io. fileInputStream; import java. util. date; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. hibernate. service. serviceRegistry; import org. hibernate. service. serviceRegistryBuilder; import org. junit. test; public class App {private SessionFactory sessionFactory = new Configuration (). configure (). addClass (User. class ). buildSessionFactory (); @ Test public void testSave () throws Exception {// save changes the temporary state to persistent State (to session management) Session session = sessionFactory. openSession (); Transaction tx = session. beginTransaction (); // ---------------------- User user = new User (); // temporary status: no corresponding record exists in the Database and no user is associated with the session. setName ("test"); session. save (user); // persistent State: In session management, a corresponding record is generated in the final database. // session. save (user); // save is executed immediately (if the primary key is specified by itself, it is also executed in flush (). delete and update are both in flush () by default () run // session. save (user); // the same object is saved multiple times, and only one is saved to the session set. The session manages the same object // session. save (user); user. setName (""); // persistent status: the database content is automatically updated. // -------------------- tx. commit (); // by default, flush () is called and all SQL statement sessions are executed. close (); // close session System. out. println (user); // Changes to the Free State after the session is closed. There are records in the database and no user is associated with the session. setName (" aaaaaa "); // modifying the object database in the Free State will not be updated} // changing the free state to the persistent State // an error will be reported if the object does not exist during the update @ Test public void testUpadet () throws Exception {Session session = sessionFactory. openSession (); Transaction tx = session. beginTransaction (); // ------------------------ User user = (User) session. get (User. class, 1); System. out. println (user); // persistent State user. setName ("Small san"); // session. flush (); // execute immediately and update it to the database System. out. println ("----------------"); // session. clear (); // clear all objects in the session, and the user changes to the Free State. // session. evict (user); // clear the user in the session, and the user changes to the Free State // session. update (user); // commit () executes the Free State to change to the persistent state System. out. println ("----------------"); // -------------------- tx. commit (); session. close () ;}@ Test public void testSaveOrUpdate () throws Exception {// changing the temporary free state to the persistent State // an error will be reported if the object does not exist during the update // This method is used to determine the object status based on the id, insert if the id is the original value (temporary state), update if it is not the original value (Free State), Session session = sessionFactory. openSession (); Transaction tx = session. beginTransaction (); // ------------------------ User user = new User (); // id Original Value // user. setId (1); // set the Id and execute the update statement. If no Id exists in the database, the user is returned. setName ("Zhang San"); session. saveOrUpdate (user); // -------------------- tx. commit (); session. close () ;}@ Test public void testDelete () throws Exception {// convert persistence and free state to delete state // If the deleted object does not exist, throw an Exception Session session = sessionFactory. openSession (); Transaction tx = session. beginTransaction (); // ------------------------ // User user = (User) session. get (User. class, 15); // session. delete (user); // delete the Persistent Object User user = new User (); user. setId (16); // simulate the session of the Free object. delete (user); // delete a free object // -------------------- tx. commit (); session. close () ;}@ Test public void testGet () {// get the object, persistence status // if the object does not exist, return null // execute the SQL statement immediately // return the real object Session = sessionFactory. openSession (); Transaction tx = session. beginTransaction (); // ------------------------ User user = (User) session. get (User. class, 1); System. out. println ("---------------------"); // ------------------------ tx. commit (); session. close () ;}@ Test public void testLoad () throws Exception {// obtain the data, which is a persistent State // delayed loading. The SQL statement is not executed immediately, in addition. class, id) is used to execute SQL statements, and database data is used to load // The returned proxy object // The delayed loading entity class cannot be final, otherwise, a subclass proxy cannot be generated. // if the data does not exist, an exception occurs. objectNotFoundException // The method that invalidates the delayed loading: Write the object class as final, or in **. hbm. configure <class... lazy = "false"> Session session = sessionFactory. openSession (); Transaction tx = session. beginTransaction (); // ------------------------ User user = (User) session. load (User. class, 1); // The returned proxy object System. out. println (user. getId (); System. out. println (user. getClass (); // System. out. println (user. getName (); // immediately execute the SQL statement // -------------------- tx. commit (); session. close () ;}@ Test public void testBatchSave () throws Exception {// to operate on a large amount of data, you need to prevent memory overflow due to excessive objects in the session Session = sessionFactory. openSession (); Transaction tx = session. beginTransaction (); // ------------------------ for (int I = 0; I <1000; ++ I) {User user = new User (); user. setName ("Big Data"); session. save (user); if (I % 10 = 0) {session. flush (); // submit the session first. clear (); // clear} // ---------------------- tx. commit (); session. close ();}}

 

Related Article

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.