我是Hibernate的新手。
尝试从数据库获取对象但出现错误:
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)
得到一个对象:
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>
最佳答案
添加Transaction tx = session.beginTransaction();
//此语句将启动交易
就在您的CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);
之前
然后在交易结束时通过调用来提交更改。
tx.commit();
关于java - org.hibernate.HibernateException : get is not valid without active transaction,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9717906/