Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manipulate objects through Hibernate

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to manipulate objects through Hibernate, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. The life cycle of Java objects in JVM

When an application creates a Java object through a new statement, JVM allocates a memory space for the object, which remains in memory as long as it is referenced by a reference variable. But if the object is not referenced by any reference variables, it ends its life cycle, and JVM's garbage collector reclaims the memory it occupies at the appropriate time.

Second, understand the cache of Session

If you want a Java object A to remain in its lifecycle, you must ensure that at least one object maintains a reference to it or can be navigated to it from other lifecycle object B. for example, a reference to object An is stored in the Java combination of object B. The implementation of the Session interface contains a series of Java collections that make up the Session cache. As shown in the following figure:

When the save () method of Session persists a Customer object, the Customer object is added to the Session cache, and even if it is not called later, as long as the Session cache is not cleared, the Customer object is still in its life cycle.

When the get () method of Session tries to load a Customer object from the database, Session first determines whether the Customer object already exists in the cache, and if so, it does not need to retrieve it from the database, but directly gets the Customer object in the cache.

Three functions of ◆ Session caching

(1) reduce the frequency of accessing the database

(2) when there is a circular association between persistent objects in the cache, Session will ensure that there will not be an endless loop to access the object graph and JVM stack overflow exceptions caused by the dead loop.

(3) to ensure that the relevant records in the database are synchronized with the corresponding objects in the cache.

The mechanism of ◆ dirty checking and cache cleaning

When a Customer object is added to the Session, Session takes a snapshot of it. When Session performs cache cleaning, it compares the current attribute of Customer with the snapshot. If there is a change, the object is called "dirty object". Session will execute the relevant SQL statement according to the object's * attribute, thus updating the database synchronously. This is the third of the three major functions of Session caching.

When the properties of objects in the Session cache change, Session does not immediately clean the cache to execute the relevant SQL statements, but cleans the cache at a specific point in time, which enables Session to combine several related SQL statements into a single SQL statement to reduce the number of visits to the database, thereby improving performance.

Third, the state of Java objects in the Hibernate persistence layer

IV. Detailed usage of Session interface

Public static void main (String [] args) throws Exception 19. {20. / / instantiate Configuration 21. Configuration conf = new Configuration (). Configure (); 22. / / instantiate SessionFactory 23. SessionFactory sf = conf.buildSessionFactory (); 24. / / instantiate Session 25. Session sess = sf.openSession (); 26. / / start transaction 27. Transaction tx = sess.beginTransaction (); 28. / / create message instance 29. News n = new News (); 30. / / set the message title and message content 31. N.setTitle ("Crazy Java Alliance formed"); 32. N.setContent ("Crazy Java Alliance has been formed, www.crazyit.org"); 33. / / Save message 34. Sess.save (n); 35. / / commit transaction 36. Tx.commit (); 37. / / close Session 38. Sess.close (); 39. }

1. Configuration class

2. SessionFactory interface

Use the SessionFactory object to get the Session object.

3. Session (session) interface

Session interface is the core interface in Hibernate, it is not HttpSession interface in Java Web application. Hibernate must get a Session object before operating the database, just like JDBC must get a Connection object before operating the database.

/ / you can also use the Session session=session.openSession () method

After obtaining the Session object, the Hibernate does not get the java.sql.Connection object that operates on the database, but waits for the Session object to CRUD and other operations on the database before it gets the java.sql.Connection object from the database connection pool. When you close the Session object, you return the java.sql.Connection object to the connection pool instead of closing the java.sql.Connection object directly.

1) save () method

The save () method is used to save the persistent object, thereby adding a piece of data to the database; instead of manipulating the database to save the data immediately after the save () method is executed, the data is not saved until after the commit () method of the Transaction object is executed.

Import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import java.util.Date; public class HibernateTest {public static void main (String [] args) {Configuration config = new Configuration (); config.configure (); SessionFactory sessionFactory = config.buildSessionFactory (); Session session = sessionFactory.getCurrentSession (); Transaction tx = session.beginTransaction () Guestbook gb = new Guestbook (); gb.setName ("Liu Wei"); gb.setPhone ("01082622285"); gb.setEmail ("liuwei8809@163.com"); gb.setTitle ("Hello everyone"); gb.setContent ("Welcome to learn Hibernate technology"). ; gb.setCreatedTime (new Date ()); session.save (gb); tx.commit (); sessionFactory.close ();}}

2) get () / load () method

You can get a record in the database through the get () method of the Session object, such as:

Guestbook gb= (Guestbook) session.get (Guestbook.class,new Integer (1)); / / find records with id 1 in Guestbook

The * parameter of the get () method is the persistent object, and the second parameter is the identifier attribute value of the persistent object to find.

You can get a record in the database through the load () method of the Session object, such as:

Guestbook gb= (Guestbook) session.load (Guestbook.class,new Integer (1)); / / find records with id 1 in Guestbook

The difference between the two: when load () and get () do not find a qualified persistence object, load () throws a violation, while the get () method returns null. In addition, the reading of cache objects is also different between the two. The load () method will first look for objects that meet the criteria from the first-level cache of Hibernate. If it cannot be found, it will go to the second-level cache of Hibernate to look for objects, and if it has not found them, it will look for them in the database; but the get () method will not look for the second-level cache of Hibernate.

3) update () method

4) delete () method

4. Transaction (transaction) interface

5. Query interface

6. Criteria interface

After reading the above, have you mastered how to manipulate objects through Hibernate? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report