Spring combined with hibernate example

Simple hibernate program

1. The first is to import the jar package of hibernate.

2. Create user and user operation record entity, log Java and user java。 The code is shown below.

Log. java

User. java

3. And create the corresponding entity configuration file, log hbm. XML and use hbm. xml。 The code is shown below.

Log. hbm. xml

4. The manager layer code is shown below.

LogManager. Java interface

Logmanagerimpl implementation

Usermanager interface

UserManagerImpl. Java implementation

5. A session is created through sessionfactory, and the commit and rollback transactions are opened and closed through the session. We encapsulate the opening and closing of the session into a tool class. HibernateUtils. The Java code is shown below.

6. Configure hibernate cfg. XML file, including database name, database associated table, user name, password, etc. The code is shown below.

7. Use JUnit for unit testing. The code is as follows.

In the above operations, the control boundary of things is at the business logic layer, In usermanagerimpl, when we call the addUser () method, we need to write this operation to the log, that is, we call the addlog () method. For class methods, one method, one session, one transaction. What if we control the transaction? We need to start the addUser () transaction and then start the addlog again () transaction? Instead of using the previous opensession method, we choose hibernateutils. Getsessionfactory() getCurrentSession(); At the same time in hibernate cfg. Getcurrentsession() is configured in XML as follows, < propertyname = "hibernate. Current_session_context_class" > thread < / property > indicates that in the current thread, it is bound with the current thread. In this way, the adduser() method and addlog() method use the same task.

What is the difference between opensession and getcurrentsession?

1. Opensession must be closed. Currentsession will be closed automatically after the transaction ends.

2. Opensession is not bound to the current thread. Currentsession is bound to the current thread. And the use of currentsession needs to be in our hibernate cfg. XML file, whether to use JDBC transaction or JTA transaction.

Hibernate and spring are used together

From the above examples, we find that hibernate transactions are independent of Hibernate's addition, deletion, modification and query of the database, and transactions are controlled in our business logic layer. For independent things, such as crosscutting problems, we naturally think of AOP. In fact, spring AOP encapsulates the management of transactions, and we are no longer responsible for opening and closing transactions with spring AOP.

Let's combine spring AOP with hibernate.

Of course, you should also import spring related jars.

Transaction management is a crosscutting issue. Modularizing transaction management is our aspect, and then configuring it in the configuration file. We can put transactions into a configuration file separately.

1. The code is as follows: ApplicationContext common XML file.

The first is to configure the sessionfactory, so that spring can get the hibernate sessionfactory to control hibernate transactions, generate sessions through the sessionfactory, and then access. In this way, hibernate's sessionfactory is injected into spring. Through the < property > tag, tell spring where the hibernate configuration file is, so that spring can read the hibernate configuration file. Secondly, configure the transaction manager, inject our sessionfactory into the transaction manager, and let the transaction manager manage transactions.

Secondly, which classes and methods execute transactions at the beginning of execution, < AOP: pointcutid = "allmanagermethod"

Secondly, configure AOP, expression = "execution (* com. Bjpowernode. Usermgr. Manager. * * (..)", Which classes are entrusted to spring to complete transaction management? We apply it to all methods in all manager packages. All classes and methods of manager participate in the operation of transactions. Where does the open transaction trigger?

Again, define a Advice to configure the propagation characteristics of the transaction, such as calling addLog () in addUser (), whether it is in the same transaction or not in the same transaction. Starting with add, beginning with del, and beginning with modify, the transaction transactional characteristics we configured are propagation= "required", so that another method is called another public thread in one method.

2. Let usermanagerimpl inherit the hibernatedao support class provided by spring.

Hibernatedaosupport, so that after inheritance, we can get the session, which is actually the session in Hibernate, but spring encapsulates it for us. We can get session: this getSession(). save(user); Or use spring encapsulated objects:

This. getHibernateTemplate(). save(user); In this way, we do not manage the opening and closing of transactions.

Previously, we used the logmanagerimpl instance in our usermanagerimpl. This time, we can use the IOC container of spring to inject the dependencies between them into spring. In this way, we can't see the instance and understand the coupling face-to-face interface programming.

The interface remains unchanged, usermanagerimpl The Java code is shown below.

LogManagerImpl. The Java code is shown below.

Delete our own hibernateutils Java class, delete hibernate cfg. Transaction configuration of getcurrentsession() in XML file.

3. Configure dependencies in the configuration file.

applicationContext-beans. The XML code is shown below.

The test program code in JUnit is shown below.

The display results are shown in the following figure.

This completes the combination of spring and hibernate, mainly using spring AOP to control hibernate transactions and spring IOC to control calls between manager layers.

summary

The above is a detailed explanation of spring combined with hibernate examples introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>