java 什么是 HibernateTemplate 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43801211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is HibernateTemplate class?
提问by Harshad_Kenjale
I am new in Hibernate currently want to implement the Hibernate Template classes , any one please tell me about the Hibernate Template classes.
我是 Hibernate 新手,目前想实现 Hibernate Template 类,请任何人告诉我 Hibernate Template 类。
xml file
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="root" />
<property name="password" value="mnrpass" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>employee.hbm.xml</value>
</list>
</property>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<bean id="springHibernateExample" class="com.javarticles.spring.hibernate.SpringHibernateExample">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
回答by Harshad_Kenjale
Copied from Hibernate Interview Questions:
Hibernate Template
When Spring and Hibernate integration started, Spring ORM provided two helper classes –
HibernateDaoSupport
andHibernateTemplate
. The reason to use them was to get theSession
from Hibernate and get the benefit of Spring transaction management. However from Hibernate 3.0.1, we can useSessionFactory getCurrentSession()
method to get the current session and use it to get the spring transaction management benefits. If you go through above examples, you will see how easy it is and that's why we should not use these classes anymore.One other benefit of HibernateTemplate was exception translation but that can be achieved easily by using
@Repository
annotation with service classes, shown in above spring mvc example. This is a trick question to judge your knowledge and whether you are aware of recent developments or not.
休眠模板
当 Spring 和 Hibernate 集成开始时,Spring ORM 提供了两个帮助类——
HibernateDaoSupport
和HibernateTemplate
. 使用它们的原因是为了Session
从 Hibernate 中获取并获得 Spring 事务管理的好处。但是从 Hibernate 3.0.1 开始,我们可以使用SessionFactory getCurrentSession()
method 来获取当前会话并使用它来获得 spring 事务管理的好处。如果你仔细阅读上面的例子,你会发现它是多么容易,这就是我们不应该再使用这些类的原因。HibernateTemplate 的另一个好处是异常翻译,但可以通过使用
@Repository
带有服务类的注释轻松实现,如上面的 spring mvc 示例所示。这是一个判断您的知识以及您是否了解最近的发展的技巧问题。
回答by Suraj Khanra
HibernateTemplateis a helper class that is used to simplify the data access code. This class supports automatically converts HibernateExceptions which is a checked exception into DataAccessExceptions which is an unchecked exception. HibernateTemplate is typically used to implement data access or business logic services. The central method is execute(), that supports the Hibernate code that implements HibernateCallback interface. Define HibernateTemplate.
HibernateTemplate是一个辅助类,用于简化数据访问代码。此类支持自动将 HibernateExceptions(已检查异常)转换为 DataAccessExceptions(未检查异常)。HibernateTemplate 通常用于实现数据访问或业务逻辑服务。中心方法是execute(),它支持实现HibernateCallback 接口的Hibernate 代码。定义 HibernateTemplate。
org.springframework.orm.hibernate.HibernateTemplate is a helper class which provides different methods for querying/retrieving data from the database. It also converts checked HibernateExceptions into unchecked DataAccessExceptions.
org.springframework.orm.hibernate.HibernateTemplate 是一个辅助类,它提供了从数据库中查询/检索数据的不同方法。它还将已检查的 HibernateExceptions 转换为未检查的 DataAccessExceptions。
HibernateTemplate benefits :
HibernateTemplate 的好处:
HibernateTemplate simplifies interactions with Hibernate Session.
The functions that are common are simplified to single method calls.
The sessions get automatically closed.
The exceptions get automatically caught and are converted to runtime exceptions.
HibernateTemplate 简化了与 Hibernate Session 的交互。
常见的函数被简化为单个方法调用。
会话会自动关闭。
异常会被自动捕获并转换为运行时异常。
Visit this link for the proper example
访问此链接以获取正确示例
http://www.javarticles.com/2015/04/spring-hibernatetempate-example.html
http://www.javarticles.com/2015/04/spring-hibernatetempate-example.html
回答by JustCode
HibernateTemplate is the class of org.springframework.orm.hibernate3. HibernateTemplate provides the integration of hibernate and spring. Spring manages database connection DML, DDL etc commands by itself. HibernateTemplate has the methods like save,update, deleteetc. Try to understand how to configure HibernateTemplate in our spring application.
HibernateTemplate 是 org.springframework.orm.hibernate3 的类。 HibernateTemplate 提供了 hibernate 和 spring 的集成。 Spring 自己管理数据库连接 DML、DDL 等命令。 HibernateTemplate 有save、update、delete等方法。尝试了解如何在我们的spring 应用程序中配置HibernateTemplate。
Add xml configuration in application.xml of spring application.
在spring应用的application.xml中添加xml配置。
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
hibernateTemplate will be used in the dao classes. Initialize hibernateTemplate. Provide a setter method to set hibernateTemplate by spring.
hibernateTemplate 将用于 dao 类。初始化 hibernateTemplate。提供一个 setter 方法来通过 spring 设置 hibernateTemplate。
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void persist(){
User u1= new User(1,"Ankita");
hibernateTemplate.save(u1);
User u2= new User(2,"Renu");
hibernateTemplate.save(u2);
}