本文介绍了hibernate session.flush with spring @transactional的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 所以我在服务层上注释了@transaction方法,而DAO层上有数据库方法查询。 @Transactional(readOnly = false) public void get(){ } 问题是当我想要将对象保存在数据库中时,我必须使用 session.flush()在DAO层方法结束时。为什么? 我想如果我注释了@transaction ,那么Spring应该在服务方法完成时自动提交事务。 DAO层: public BaseEntity saveEntity(BaseEntity entity)throws Exception { try { Session session = sessionFactory.getCurrentSession(); session.saveOrUpdate(entity); session.flush(); catch(HibernateException he){抛出新的异常(无法保存实体+实体); } 返回实体; 服务层: @Transactional(readOnly = false) public BaseEntity saveEntity(BaseEntity entity)throws Exception { return dao.saveEntity(entity); spring配置: < context:property-placeholder properties-ref =deployProperties/> < tx:注解驱动的事务管理器=transactionManager/> < jpa:repositories base-package =com/> < bean id =dataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSource destroy-method =closep:driverClass =$ {app.jdbc。 p:jdbcUrl =$ {app.jdbc.url}p:user =$ {app.jdbc.username}p:password =$ {app.jdbc .password}p:acquireIncrement =5p:idleConnectionTestPeriod =60p:maxPoolSize =100p:maxStatements =50p:minPoolSize =10/> < bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBeanp:persistenceXmlLocation =classpath *:META-INF / persistence.xmlp:persistenceUnitName =hibernatePersistenceUnitp:dataSource-ref =dataSourcep:jpaVendorAdapter-ref =hibernateVendor/> < bean id =sessionFactory class =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBeanp:dataSource-ref =dataSourcep:configLocation =$ {hibernate.config}p:packagesToScan =com/> p:showSql =false/> < bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager p:entityManagerFactory-ref =entityManagerFactory/> 解决方案是的,如果您有 @Transactional 为你的DAO方法,那么你不需要手动刷新会话,如果方法中的操作成功,hibernate将会照顾会话作为提交事务的一部分。 检查此链接以了解@Transactional的工作方式 - Spring - @Transactional - 在后台发生了什么? I am using spring and hibernate in my application and using spring transaction.So i have service layer with annotation @transaction on methods and DAO layer having methods for database query.@Transactional(readOnly = false)public void get(){}The issue is when i want to save an object in database,then i have to use session.flush() at the end of DAO layer method.Why?I think if i have annotated @transaction,then spring should automatic commit the transaction on completion of service method.DAO layer : public BaseEntity saveEntity(BaseEntity entity) throws Exception { try { Session session = sessionFactory.getCurrentSession(); session.saveOrUpdate(entity); session.flush(); } catch (HibernateException he) { throw new Exception("Failed to save entity " + entity); } return entity; }Service layer : @Transactional(readOnly = false) public BaseEntity saveEntity(BaseEntity entity) throws Exception { return dao.saveEntity(entity); }spring config : <context:property-placeholder properties-ref="deployProperties" /> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- Activate Spring Data JPA repository support --> <jpa:repositories base-package="com" /> <!-- Declare a datasource that has pooling capabilities--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="${app.jdbc.driverClassName}" p:jdbcUrl="${app.jdbc.url}" p:user="${app.jdbc.username}" p:password="${app.jdbc.password}" p:acquireIncrement="5" p:idleConnectionTestPeriod="60" p:maxPoolSize="100" p:maxStatements="50" p:minPoolSize="10" /> <!-- Declare a JPA entityManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:persistenceXmlLocation="classpath*:META-INF/persistence.xml" p:persistenceUnitName="hibernatePersistenceUnit" p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="hibernateVendor"/> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="${hibernate.config}" p:packagesToScan="com" /> <!-- Specify our ORM vendor --> <bean id="hibernateVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="false"/> <!-- Declare a transaction manager--> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"/> 解决方案 Yes, if you have @Transactional for your DAO method then you need not flush the session manually, hibernate will take care of flushing the session as part of committing the transaction if the operations in the method are successful.Check this link to know on how @Transactional works - Spring - @Transactional - What happens in background? 这篇关于hibernate session.flush with spring @transactional的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-04 06:48