[英]Hibernate saving entity not working
I am using Spring 3.2 mvc
and Hibernate 4
in my project. 我在项目中使用
Spring 3.2 mvc
和Hibernate 4
。
hibernate.cfg.xml 的hibernate.cfg.xml
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="show_sql">true</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.validator.apply_to_ddl">false</property>
<property name="hibernate.validator.autoregister_listeners">false</property>
servlet-context.xml servlet的context.xml中
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<security:global-method-security pre-post-annotations="enabled"/>
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.abc" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
DaoImpl Class : DaoImpl类别 :
public void add(Entity entity) {
try {
this.sessionFactory.getCurrentSession().save(entity);
this.sessionFactory.getCurrentSession().flush();
} catch (Exception e) {
logger.error("Exception occured " + e);
}
}
This is my project configuration
and dao impl class
file. 这是我的
project configuration
和dao impl class
文件。
root-context.xml 根的context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.abc" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- <property name = "dataSource" ref = "dataSource"></property> -->
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="entityInterceptor" ref ="auditLogInterceptor"/>
</bean>
Issue 问题
As of now in hibernate.cfg.xml, I have mentioned hibernate.connection.autocommit
= true and in daoimpl while saving entity I need to call flush
after .save
. 到目前为止,在hibernate.cfg.xml中,我已经提到过
hibernate.connection.autocommit
= true,并且在daoimpl中保存实体时,我需要在.save
之后调用flush
。
If I remove hibernate.connection.autocommit
= true and .flush
from daoimpl class, I observed that .save
method in daoimpl is not working, means my data is not inserting and even I cannot see insert query executed by hibernate on console. 如果我从daoimpl类中删除了
hibernate.connection.autocommit
= true和.flush
,我发现daoimpl中的.save
方法无法正常工作,这意味着我的数据无法插入,甚至我也看不到hibernate在控制台上执行的插入查询。
hibernate.connection.autocommit
= true should not be there in hibernate cfg xml as if I doing operation on multiple table in same transaction and if some error occurred then rollback will not happen. hibernate.connection.autocommit
cfg xml中不应存在hibernate.connection.autocommit
= true,就好像我在同一事务中对多个表进行操作一样,如果发生某些错误,则不会发生回滚。
I want that .save
in daoimpl should work even I don't write hibernate.connection.autocommit
= true in hibernate cfg xml and .flush
. 我希望即使我不在hibernate cfg xml和
.flush
编写hibernate.connection.autocommit
= true,daoimpl中的.save
也应能正常工作。
I am using @Transactional annotation for transaction. 我正在使用@Transactional注释进行交易。
You haven't added any TransactionManager to your configuration: 您尚未将任何TransactionManager添加到配置中:
Remove the following properties: 删除以下属性:
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.autocommit">true</property> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.timeout">300</property> <property name="hibernate.c3p0.max_statements">50</property> <property name="hibernate.c3p0.idle_test_period">3000</property>
Add a connection pooling DataSource (DBCP2 is a much better alternative than C3P0) 添加连接池数据源(DBCP2比C3P0更好)
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" oracle.jdbc.driver.OracleDriver"/> <property name="url" value="your-oracle-driver-url"/> <property name="username" value="your-username"/> <property name="password" value="your-password"/> </bean>
Now add the Sessionfactory Spring proxy: 现在添加Sessionfactory Spring代理:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean>
Add the TransactionManager bean 添加TransactionManager bean
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean>
Update 更新
If you have the transaction manager set in a separate spring application context (eg root-context.xml), then move these lines from your web context to where the back-end context: 如果您在单独的spring应用程序上下文(例如root-context.xml)中设置了事务管理器,则将这些行从Web上下文移至后端上下文:
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.abc.service" />
<context:component-scan base-package="com.abc.dao" />
And only allow the web context to scan its own beans: 并且仅允许Web上下文扫描其自己的bean:
<context:component-scan base-package="com.abc.web" />
It's not good to mix the web and the back-end contexts responsibilities . 混合使用网络和后端上下文职责是不好的 。
My problem solved, I just wrote the annotation @Transactional in the 我的问题解决了,我只在注解@Transactional中写了
@Repository
@Transactional
public class AbstractHibernateDao<T extends Serializable> {
private Class<T> clazz;
@Autowired
private SessionFactory sessionFactory;
And that solved that I couldnt Delete or Save without using Flush. 这就解决了我不使用冲洗就无法删除或保存的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.