org.hibernate.HibernateException:如果没有活动事务,get无效 [英] org.hibernate.HibernateException: get is not valid without active transaction
本文介绍了org.hibernate.HibernateException:如果没有活动事务,get无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 自动创建hibernate.cfg.xml(Netbeans向导)
- 自动创建HibernateUtil.java
- 自动创建带注释的POJO类
Trying从数据库中获取对象但收到错误:
线程pool-1-thread-1中的异常org.hibernate.HibernateException :get没有活动事务是有效的
在org.hibernate.context.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
获取对象:
会话会话= HibernateUtil.getSessionFactory()。getCurrentSession() ;
CallInfo ci =(CallInfo)session.get(CallInfo.class,ucid);
hibernate.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>根< / property>
< property name =hibernate.connection.password>根< / 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>线程< / property>
解决方案
添加
Transaction tx = session.beginTransaction();
//此语句将启动交易
就在你的 CallInfo ci =(CallInfo)session.get(CallInfo.class,ucid);
您的交易结束通过调用提交更改。
tx.commit();
I'm new to Hibernate.
- Automatically created hibernate.cfg.xml (Netbeans wizard)
- Automatically created HibernateUtil.java
- Automatically created POJO class with annotations
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
<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>
解决方案
Add
Transaction tx = session.beginTransaction();
//This statement will initiate the transaction
just before your CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);
and at the end of your transaction commit the changes by calling..
tx.commit();
这篇关于org.hibernate.HibernateException:如果没有活动事务,get无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文