解决load,get,save 等is not valid without active transaction异常的原因。

简介: org.hibernate.HibernateException:*** is not valid without active transaction 2010-12-09 15:36:07标签:休闲is not valid without active 生活Hibernate职场 Exception in thread "main" org.hibernate.HibernateExc
org.hibernate.HibernateException:*** is not valid without active transaction 2010-12-09 15:36:07

Exception in thread "main" org.hibernate.HibernateException: *** is not valid without active transaction
 at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
 at $Proxy13.getListeners(Unknown Source)
 at org.hibernate.search.backend.impl.EventSourceTransactionContext.getIndexWorkFlushEventListener(EventSourceTransactionContext.java:78)
 at org.hibernate.search.backend.impl.EventSourceTransactionContext.<init>(EventSourceTransactionContext.java:41)
 at org.hibernate.search.impl.FullTextSessionImpl.<init>(FullTextSessionImpl.java:75)
 at org.hibernate.search.Search.getFullTextSession(Search.java:23)
 at com.yin.hibernate.model.GuestBookTest.main(GuestBookTest.java:28)

在网上找的原因是:
如果使用hibernate的getCurrentSession()获得session对象,对查询语句也需要开启事务,不然就会抛出如上异常!
解决方法

加上  Transaction tx = session.beginTransaction(); 
      tx.commit();
注意:在 hibernate.cfg.xml
如果采用jdbc独立引用程序配置如下:
<property name=”hibernate.current_session_context_class”>thread</property>
如果采用了JTA事务配置如下
<property name=”hibernate.current_session_context_class”>jta</property>

目录
相关文章
|
6月前
|
Oracle 关系型数据库 数据库
Active Data Guard Real-Time Cascade
12c 的 Cascaded Standby 数据库
60 7
加载模型出现-RuntimeError: Error(s) in loading state_dict for Net:unexpected key(s) in state_dict: XXX
加载模型出现-RuntimeError: Error(s) in loading state_dict for Net:unexpected key(s) in state_dict: XXX
584 0
|
网络安全 开发工具
【解决方案】A session ended very soon after starting. Check that the command in profile “XXX” is correct.
【解决方案】A session ended very soon after starting. Check that the command in profile “XXX” is correct.
1137 0
【解决方案】A session ended very soon after starting. Check that the command in profile “XXX” is correct.
|
数据可视化 PyTorch 算法框架/工具
Pychram Pytorch Tensorboard 报错 “No dashboards are active for the current data set.“ 解决方案
Pychram Pytorch Tensorboard 报错 “No dashboards are active for the current data set.“ 解决方案
Pychram Pytorch Tensorboard 报错 “No dashboards are active for the current data set.“ 解决方案
|
存储 NoSQL Linux
redis异常 Commands that may modify the data set are disabled, because this instance is
MISCONF Redis配置为保存RDB快照,但目前无法在磁盘上持久化。可能修改数据集的命令被禁用,因为该实例被配置为在RDB快照失败时报告错误(stop-write -on-bgsave-error选项)。请检查Redis的日志RDB错误的详细信息.
redis异常 Commands that may modify the data set are disabled, because this instance is
|
安全
Error Code: 1175. You are using safe update mode and you tried to update a t
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/inforstack/article/details/79677217 在安全模式下,只能根据主键来做修改,所以使用非主键修改,那么将要解除安全模式,然后再执行操作。
1661 0
|
SQL Oracle 关系型数据库