Hibernate中的事务怎么用

发布时间:2021-12-06 09:09:52 作者:小新
来源:亿速云 阅读:148

小编给大家分享一下Hibernate中的事务怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

Hibernate事务的使用:

一、要使用事务?

事务是单个的工作单元。如果某一事务成功,则在该事务中进行的所有数据修改均会提交,成为数据库中的***组成部分。如果事务遇到错误且必须取消或回滚,则所有数据修改均被清除。
(查询中一般可以不用事务!除非查询和修改是合成一个事务的操作)

二、不用事务?(今天重点说这里)
Hibernate每次都更新数据库,并且使用了。
1、Hibernate事务会自动更新缓存。其意思是指,缓存中查出来的所有对象(一个事务内),都判断对象是否被更新。如果更新了,Hibernate会自动更新数据库。

事务开始.

假设我现在查出所有用户,并对某些用户进行了操作,但这些操作我不想更新数据库,也就是我只是操作缓存,来计算他们的奖金。

计算完成奖金后,该方法结束。

事务结束.

以上操作的结果是,数据库记录被更新了。因为在一个事务里,但其实我只是想试操作用户的数据而不想更新。但为什么更新了,这就要问Hibernate了,对是这家伙干的好事,Hibernate发现查出了所有用户的List,并把所有用户都放在Hibernate缓存中了。如果在一个事务中,你对该List里的用户操作了,并且他发现了你的操作,他会自动更新数据库。

但我现在不想让他更新数据库,并且我的计算奖金算法看上去没有必要用事务。是的他根本没有必要用试务,因为他不更新,不保存,他只是算奖金。

所以解决这个问题的方法就是不要用管理器中对该Service进行管理.

现在想想为什么我们不用事务,Hibernate就不自动更新数据库了?原因很简单,因为当我们查出所有用户之后Hibernate还做了件事,Session.close();
当他调用了Session.close()方法时,我们查询的List就变成了游离对象了,这时该List已不在Hibernate缓存中,这时你可以方便的设置和更新了,只不手动设用Update

Hibernate是不会帮你更新的^_^。当然也可以使用evict方法把对象变为游离。

呵呵,可是在一个事务中,就算你把他变成游离,Hibernate还是会自动更新的。(我试过)不过估计Hibernate应该有设置在一个事务内不自动更新的参数吧?

三、Hibernate事Spring事务配置说明:

我下面用的是[指定对满足哪些beanname的bean自动生成事务代理],这里看清楚点是name,也就是说这样写:
userInfoService
表示在Spring上下文中有userInfoService实例,并且这个实例必需对应这样一个接口UserInfoService,这时Spring才会自动为其配置Hibernate事务,否则会报错

[org.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[$Proxy14]torequiredtype],错误的意思是找不到对应的接口。

四、下面是我的Spring配置文件

com/insurance/model/base/UserInfo.hbm.xmlcom/insurance/model/base/Base.hbm.xmlcom/insurance/model/base/AdminInfo.hbm.xmlcom/insurance/model/base/CompanyInfo.hbm.xmlcom/insurance/model/base/BxCom.hbm.xmlcom/insurance/model/base/XzInfo.hbm.xmlcom/insurance/model/base/ClientInfo.hbm.xmlcom/insurance/model/base/UserGrade.hbm.xmlcom/insurance/model/base/UserMoneyLog.hbm.xmlcom/insurance/model/base/SxBdInfo.hbm.xmlcom/insurance/model/base/CxBdInfo.hbm.xmlcom/insurance/model/base/CarBdInfo.hbm.xmlcom/insurance/model/base/SelectBase.hbm.xmlcom/insurance/model/base/PayInfo.hbm.xmlcom/insurance/model/base/AwardInfo.hbm.xml${hibernate.dialect}true${hibernate.show_sql}org.hibernate.hql.classic.ClassicQueryTranslatorFactory${hibernate.hbm2ddl.auto}${hibernate.jdbc.batch_size}${hibernate.jdbc.fetch_size}PROPAGATION_REQUIRED,readOnlyPROPAGATION_REQUIREDuserInfoServiceadminInfoServicepageServicecompanyInfoServicexzInfoServicebaseServiceclientInfoServiceuserGradeServiceuserMoneyLogServicesxBdInfoServicecxBdInfoServicecarBdInfoServicetransactionInterceptor五、下面是详细的错误信息 2008-05-2309:07:22,046[org.springframework.web.context.ContextLoader]-[ERROR]Contextinitializationfailed org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'accountAddPay'definedinServletContextresource[/WEB- INF/classes/applicationContext.xml]:Errorsettingpropertyvalues;nestedexceptionisorg.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException(1errors);nestedpropertyAccessExceptionsare:[org.springframework.beans.TypeMismatchException:Failedtoconvert propertyvalueoftype[$Proxy14]torequiredtype[com.insurance.service.pay.AccountSellPay]forproperty'accountSellPay'] PropertyAccessExceptionsException(1errors) org.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[$Proxy14]torequiredtype [com.insurance.service.pay.AccountSellPay]forproperty'accountSellPay' atorg.springframework.beans.BeanWrapperImpl.doTypeConversionIfNecessary(BeanWrapperImpl.java:951) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:692) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:572) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:737) atorg.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:764) atorg.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:753) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1057) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:857) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:378) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145) atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:283) atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313) atorg.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139) atorg.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246) atorg.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184) atorg.springframework.web.context.ContextLoaderServlet.init(ContextLoaderServlet.java:83) atjavax.servlet.GenericServlet.init(GenericServlet.java:211) atorg.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1019) atorg.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862) atorg.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3991) atorg.apache.catalina.core.StandardContext.start(StandardContext.java:4335) atorg.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823) atorg.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807) atorg.apache.catalina.core.StandardHost.addChild(StandardHost.java:595) atorg.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277) atorg.apache.catalina.core.StandardHost.install(StandardHost.java:832) atorg.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:683) atorg.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:432) atorg.apache.catalina.startup.HostConfig.start(HostConfig.java:964) atorg.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349) atorg.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091) atorg.apache.catalina.core.StandardHost.start(StandardHost.java:789) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083) atorg.apache.catalina.core.StandardEngine.start(StandardEngine.java:478) atorg.apache.catalina.core.StandardService.start(StandardService.java:476) atorg.apache.catalina.core.StandardServer.start(StandardServer.java:2298) atorg.apache.catalina.startup.Catalina.start(Catalina.java:556) atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) atjava.lang.reflect.Method.invoke(Method.java:585) atorg.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284) atorg.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422) 调用父类构造方法...! 2008-05-2309:07:22,656[org.apache.struts.validator.ValidatorPlugIn]-[INFO]Loadingvalidationrulesfilefrom'/WEB-INF/validator-rules.xml' 2008-05-2309:07:22,656[org.apache.struts.validator.ValidatorPlugIn]-[INFO]Loadingvalidationrulesfilefrom'/WEB-INF/validation.xml' 2008-05-2309:07:22,906[org.springframework.web.struts.ContextLoaderPlugIn]-[INFO]ContextLoaderPlugInforStrutsActionServlet'action,module'': initializationstarted 2008-05-2309:07:22,906[org.springframework.web.struts.ContextLoaderPlugIn]-[ERROR]Contextinitializationfailed org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'accountAddPay'definedinServletContextresource[/WEB- INF/classes/applicationContext.xml]:Errorsettingpropertyvalues;nestedexceptionisorg.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException(1errors);nestedpropertyAccessExceptionsare:[org.springframework.beans.TypeMismatchException:Failedtoconvert propertyvalueoftype[$Proxy14]torequiredtype[com.insurance.service.pay.AccountSellPay]forproperty'accountSellPay'] PropertyAccessExceptionsException(1errors) org.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[$Proxy14]torequiredtype [com.insurance.service.pay.AccountSellPay]forproperty'accountSellPay' atorg.springframework.beans.BeanWrapperImpl.doTypeConversionIfNecessary(BeanWrapperImpl.java:951) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:692) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:572) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:737) atorg.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:764) atorg.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:753) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1057) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:857) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:378) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145) atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:283) atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313) atorg.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139) atorg.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246) atorg.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184) atorg.springframework.web.context.ContextLoaderServlet.init(ContextLoaderServlet.java:83) atjavax.servlet.GenericServlet.init(GenericServlet.java:211) atorg.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1019) atorg.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862) atorg.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3991) atorg.apache.catalina.core.StandardContext.start(StandardContext.java:4335) atorg.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823) atorg.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807) atorg.apache.catalina.core.StandardHost.addChild(StandardHost.java:595) atorg.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277) atorg.apache.catalina.core.StandardHost.install(StandardHost.java:832) atorg.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:683) atorg.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:432) atorg.apache.catalina.startup.HostConfig.start(HostConfig.java:964) atorg.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349) atorg.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091) atorg.apache.catalina.core.StandardHost.start(StandardHost.java:789) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083) atorg.apache.catalina.core.StandardEngine.start(StandardEngine.java:478) atorg.apache.catalina.core.StandardService.start(StandardService.java:476) atorg.apache.catalina.core.StandardServer.start(StandardServer.java:2298) atorg.apache.catalina.startup.Catalina.start(Catalina.java:556) atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) atjava.lang.reflect.Method.invoke(Method.java:585) atorg.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284) atorg.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422) javax.servlet.UnavailableException atorg.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:880) atorg.apache.struts.action.ActionServlet.init(ActionServlet.java:336) atcom.insurance.struts.action.ActionInit.init(ActionInit.java:34) atjavax.servlet.GenericServlet.init(GenericServlet.java:211) atorg.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1019) atorg.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862) atorg.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3991) atorg.apache.catalina.core.StandardContext.start(StandardContext.java:4335) atorg.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823) atorg.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807) atorg.apache.catalina.core.StandardHost.addChild(StandardHost.java:595) atorg.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277) atorg.apache.catalina.core.StandardHost.install(StandardHost.java:832) atorg.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:683) atorg.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:432) atorg.apache.catalina.startup.HostConfig.start(HostConfig.java:964) atorg.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349) atorg.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091) atorg.apache.catalina.core.StandardHost.start(StandardHost.java:789) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083) atorg.apache.catalina.core.StandardEngine.start(StandardEngine.java:478) atorg.apache.catalina.core.StandardService.start(StandardService.java:476) atorg.apache.catalina.core.StandardServer.start(StandardServer.java:2298) atorg.apache.catalina.startup.Catalina.start(Catalina.java:556) atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) atjava.lang.reflect.Method.invoke(Method.java:585) atorg.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284) atorg.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422) 2008-5-239:07:23org.apache.coyote.http11.Http11Protocolstart 信息:StartingCoyoteHTTP/1.1onhttp-8080 2008-5-239:07:23org.apache.jk.common.ChannelSocketinit 信息:JK2:ajp13listeningon/0.0.0.0:8009 2008-5-239:07:23org.apache.jk.server.JkMainstart 信息:JkrunningID=0time=0/32config=D:\Tomcat5.0\conf\jk2.properties 2008-5-239:07:23org.apache.catalina.startup.Catalinastart 信息:Serverstartupin6953ms

以上是“Hibernate中的事务怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Redis事务怎么用
  2. 深入浅析Java中的Hibernate事务管理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

hibernate

上一篇:Hibernate与MyBatis有什么优势

下一篇:mysql函数中的GROUP_CONCAT是怎样用的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》