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 use Hibernate Callback

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use Hibernate Callback". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Hibernate Callback.

Purpose: use HibernateTemplate to execute the execute (new Hibernate Callback ()) method, get the session from Hibernate Callback, do multiple operations in this session, and want them to be in the same transaction.

If you write like this:

Public static void main (String ss []) {CtxUtil.getBaseManager () .getHibernateTemplate () .execute (new HibernateCallback () {public Object doInHibernate (Session session) throws HibernateException, SQLException {/ / Save stu1 Student stu1 = new Student (); stu1.setName ("aaaa"); / / in the database, the name field is not allowed to be null session.save (stu1); session.flush () / / in fact, if the programmer does not "itch" to call this flush (), the transaction of session in HibernateTemplate is still very convenient: Student stu2 = new Student (); session.save (stu2); / / if the name field is not set, an exception session.flush (); return null;}} is expected.

It's a good idea to expect spring to commit transactions when session is closed after performing the execute callback, but spring will not do so. Let's take a look at what session.beginTransation () does in Hibernate's source code. Look at the following code:

Public Transaction beginTransaction () throws HibernateException {errorIfClosed (); if (rootSession! = null) {/ / todo: should seriously consider not allowing a txn to begin from a child session / / can always route the request to the root session log.warn ("Transaction started on non-root session");} Transaction result = getTransaction (); result.begin (); return result;}

The result in this method is an instance of org.hibernate.transaction.JDBCTransaction, and the source code of the getTransaction () method in the method is:

Public Transaction getTransaction () throws HibernateException {

If (hibernateTransaction==null) {

Log.error (owner.getFactory (). GetSettings (). GetTransactionFactory (). GetClass ())

HibernateTransaction = owner.getFactory (). GetSettings ().

GetTransactionFactory () .createTransaction (this owner)

}

Return hibernateTransaction

}

Trace again, owner.getFactory (). The source code of the createTransaction () method for getSettings () .getTransactionFactory () is as follows:

Public Transaction createTransaction

(JDBCContext jdbcContext, Context transactionContext)

Throws HibernateException {

Return new JDBCTransaction (jdbcContext, transactionContext)

}

It returns a JDBCTransaction, nothing special. The Hibernate Callback method is described above.

Thank you for your reading, the above is the content of "how to use Hibernate Callback", after the study of this article, I believe you have a deeper understanding of how to use Hibernate Callback, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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