【发布时间】:2012-03-15 10:30:29
【问题描述】:
我是 Hibernate 的新手。
- 自动创建 hibernate.cfg.xml(Netbeans 向导)
- 自动创建 HibernateUtil.java
- 自动创建带有注释的 POJO 类
试图从数据库中获取对象但出现错误:
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>
【问题讨论】:
-
能否添加调用get()方法的代码?
-
@ShashankKadne
CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid); -
添加“交易 tx = session.beginTransaction();”就在这一行之前和操作结束时调用“tx.commit();”
-
@ShashankKadne 谢谢,它有效
-
:我会添加它作为答案然后...
标签: java hibernate netbeans netbeans-7