Vex*_*toR 9 java netbeans hibernate netbeans-7
我是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)
Run Code Online (Sandbox Code Playgroud)
得到一个对象:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);
Run Code Online (Sandbox Code Playgroud)
的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>
Run Code Online (Sandbox Code Playgroud)
Sha*_*dne 14
加
Transaction tx = session.beginTransaction(); //此语句将启动事务
就在你之前 CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);
并在您的事务结束时通过调用提交更改..
tx.commit();
Run Code Online (Sandbox Code Playgroud)
另一种解决方案是使用openSession()而不是getCurrentSession().然后,只有在更新查询时才需要使用事务.
Session session = HibernateUtil.getSessionFactory().openSession();
CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25787 次 |
| 最近记录: |