Java org.hibernate.HibernateException:如果没有活动事务,则保存无效
我正在创建Java org.hibernate.HibernateException:如果没有活动事务,则保存无效,java,mysql,hibernate,persistence,Java,Mysql,Hibernate,Persistence,我正在创建JSF应用程序,并在其中使用一些hibernate的东西。我想做的就是将实体保存到数据库中,但我一直遇到以下异常: org.hibernate.HibernateException: save is not valid without active transaction org.hibernate.HibernateException: No CurrentSessionContext configured! 起初,我遇到了这样一个例外: org.hibernate.Hibern
JSF
应用程序,并在其中使用一些hibernate的东西。我想做的就是将实体保存到数据库中,但我一直遇到以下异常:
org.hibernate.HibernateException: save is not valid without active transaction
org.hibernate.HibernateException: No CurrentSessionContext configured!
起初,我遇到了这样一个例外:
org.hibernate.HibernateException: save is not valid without active transaction
org.hibernate.HibernateException: No CurrentSessionContext configured!
然后我发现我需要将此添加到我的hibernate配置中:
<property name="hibernate.current_session_context_class">thread</property>
我的hibernate.cfg.xml文件如下所示:
public void create(T entity) {
getSessionFactory().getCurrentSession().save(entity);
}
<hibernate-configuration>
<session-factory>
<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/online_tests_management</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.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.groupgti.onlinetests.management.db.Service"/>
</session-factory>
</hibernate-configuration>
public void create(T entity) {
org.hibernate.Session ss= getSessionFactory().getCurrentSession();
Transaction tx=ss.beginTransaction();
ss.save(entity);
tx.commit();
}
org.hibernate.dialogue.mysqldialogue
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/online_tests_management
根
根
真的
更新
线
我正在使用:
- Hibernate-4.1.4.Final
- JDK1.6
- 雄猫6
- JSF2.0
- 第3.3.1条
- MySql
有人知道问题出在哪里吗?您必须调用
session.beginTransaction()
尝试将您的方法更改为如下所示:
public void create(T entity) {
getSessionFactory().getCurrentSession().beginTransaction();
getSessionFactory().getCurrentSession().save(entity);
getSessionFactory().getCurrentSession().endTransaction();
}
应该可以解决你的问题。这样做:
public void create(T entity) {
getSessionFactory().getCurrentSession().save(entity);
}
<hibernate-configuration>
<session-factory>
<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/online_tests_management</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.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.groupgti.onlinetests.management.db.Service"/>
</session-factory>
</hibernate-configuration>
public void create(T entity) {
org.hibernate.Session ss= getSessionFactory().getCurrentSession();
Transaction tx=ss.beginTransaction();
ss.save(entity);
tx.commit();
}
你自己做异常处理部分。我想你会发现这样更健壮、更合适:
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// Do some work like:
//session.load(...);
//session.persist(...);
//session.save(...);
tx.commit(); // Flush happens automatically
}
catch (RuntimeException e) {
tx.rollback();
throw e; // or display error message
}
finally {
session.close();
}
您不能“关闭”事务,您可以关闭会话,如您所见。请阅读,它可能对您有用。小心,应避免使用“openSession”。正确的方法是“getCurrentSession”。我通过SpringAOP使用SpringHibernateTransactionManager来配置事务。我可以在日志中看到事务正常启动,但我的保存仍然失败,因为我没有使用上述代码。为什么呢?