gpt4 book ai didi

java - 组织.hibernate.HibernateException : get is not valid without active transaction

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:08:54 26 4
gpt4 key购买 nike

我是 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>

最佳答案

添加

Transaction tx = session.beginTransaction();//这条语句会发起交易

就在您的 CallInfo ci = (CallInfo) session.get(CallInfo.class, ucid); 之前

并在您的交易结束时通过调用提交更改..

tx.commit();

关于java - 组织.hibernate.HibernateException : get is not valid without active transaction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9717906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com