我是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(); //此语句将启动交易
Transaction tx = session.beginTransaction();
就在你的 CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);
CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);
然后在交易结束时通过调用提交更改。
tx.commit();