In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail why Spring is used to manage Hibernate. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Why use the Hibernate framework?
Since the Hibernate framework is used to access the management persistence layer, why is it mentioned to use Spring to manage and integrate Hibernate?
First, let's take a look at the steps that Hibernate takes. For example, a step to add a user. In order to explain the questions raised here,
Looking at the screenshot, you can see that the steps of Hibernate operation are as follows:
Get the Configuration object
Create SessionFactory
Create Session
Open transaction
Perform a persistence operation. For example, add user action above
Commit transaction
An exception occurs and the transaction is rolled back
Close the transaction
Using Hibernate to access the persistence layer, these eight steps are performed each time. But what if you use Spring to manage Hibernate?
First, Spring provides the HibernateTemple class for Hibernate. This template class encapsulates session. And Spring defaults to auto-commit transactions. In the dao layer, you can write this.save (Object) directly.
In practice, I encountered a problem that could not be inserted into the database. After some twists and turns, many experiments, verification, and summing up the reasons, the results are as follows:
Note: all operations must be done in a transaction. If you simply use Hibernate, the transaction needs to manually turn on the commit close operation. Of course, it can also be submitted automatically in the Hibernate configuration file.
If you use Spring to manage Hibernate, the default is to automatically commit transactions. Here, however, it is important to note that if you use Spring to configure the data source in the configuration file instead of hibernate.cfg.xml, you can successfully insert the database because HibernateTemple provides autocommit transactions by default. The configuration data source in the configuration file of Spring is as follows: the jar required to use this data source is as follows: commons-dbcp.jar,commons-pool.jar,msbase.jar,mssqlserver.jar, msutil.jar,sqljdbc4.jar
Org.hibernate.dialect.SQLServerDialect update!-if you use the traditional hbm.xml method, you can simplify the steps of registering hbm.xml in the following ways, and class is LocalSessionFactoryBean-- > classpath:User.hbm.xml
If the data source is not configured directly in Spring, but hibernate.cfg.xml is used, it cannot be inserted into the database if it cannot be directly this.save (Object), but can be saved in the cache. Because all operations are performed in a transaction, hibernate.cfg.xml data sources are not auto-committed by default. The solution is as follows: you can enter it in hibernate.cfg.xml:
True
The following Spring uses hibernate.cfg.xml data sources
Classpath:hibernate.cfg.xml classpath:User.hbm.xml
The advantage of managing Hibernate with Spring described above is that you don't have to manually create SessionFactory,Session or manually open commit and close transactions, all of which are completely managed by Spring. And Spring also provides HibernateTemple tools to make everything simple and convenient.
The dao layer is configured in the Spring configuration file as follows:
The code in the dao layer is as follows:
Package com.UserDao; import org.springframework.orm.hibernate3.HibernateTemplate; import com.user.User; public class UserDaoImpl extends HibernateTemplate implements UserDao {@ Override public void insert (User user) {this.save (user);}}
This is just the key code.
On why to use Spring to manage Hibernate to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.