Java org.hibernate.HibernateException: get 在没有活动事务的情况下无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9717906/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:38:31  来源:igfitidea点击:

org.hibernate.HibernateException: get is not valid without active transaction

javahibernatenetbeansnetbeans-7

提问by VextoR

I'm new to Hibernate.

我是 Hibernate 的新手。

  • Automatically created hibernate.cfg.xml (Netbeans wizard)
  • Automatically created HibernateUtil.java
  • Automatically created POJO class with annotations
  • 自动创建的 hibernate.cfg.xml(Netbeans 向导)
  • 自动创建的 HibernateUtil.java
  • 自动创建带注释的 POJO 类

Trying to get object from database but getting error:

试图从数据库中获取对象但出现错误:

Exception in thread "pool-1-thread-1" org.hibernate.HibernateException: get is not valid without active transaction
    at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)

getting an object:

获取对象:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);

hibernate.cfg.xml

休眠文件.cfg.xml

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sochi_feedback</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.current_session_context_class">thread</property>

采纳答案by Shashank Kadne

Add

添加

Transaction tx = session.beginTransaction();//This statement will initiate the transaction

Transaction tx = session.beginTransaction();//这条语句将发起交易

just before your CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);

就在你之前 CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);

and at the end of your transaction commit the changes by calling..

并在您的事务结束时通过调用提交更改..

tx.commit();

回答by Amil Waduwawara

Another solution is to use openSession()instead of getCurrentSession(). Then transactions can be used only when required for updating queries.

另一种解决方案是使用openSession()代替getCurrentSession(). 那么事务只能在需要更新查询时使用。

Session session = HibernateUtil.getSessionFactory().openSession();
CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);

回答by prasad

Even after beginTransaction()and and commit()if you still get the

即使在beginTransaction()commit()如果你仍然得到

Caused by: org.hibernate.HibernateException: setDefaultReadOnly is not valid without active transaction
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) 

go to "Start" and search for services and restart the database service

转到“开始”并搜索服务并重新启动数据库服务

回答by Farruh Habibullaev

Before you actually start the transaction you need to start the session by calling session.beginTransaction(), right after you create sessionFactory.

在您实际开始事务之前,您需要session.beginTransaction()在创建 sessionFactory 之后立即调用 来启动会话。