gpt4 book ai didi

java - 组织.hibernate.TransactionException : Unable to locate JTA UserTransaction

转载 作者:行者123 更新时间:2023-11-30 08:48:10 24 4
gpt4 key购买 nike

我正在编写我的第一个使用 Hibernate Framework 插入记录的示例。

首先,我尝试按照 URL 给出的说明进行操作

你可以在 GIT Hibernate Basic 查看我的代码

当调用下面一段代码时

public Long saveEmployee(Employee emp){
Session session = new Configuration().configure().buildSessionFactory().openSession();
session.beginTransaction();
Long id = (Long) session.save(emp);
session.getTransaction().commit();
session.close();
return id;
}

hibernate .cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root123</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- validate | update | create | create-drop -->
<!-- validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session. -->
<property name="hbm2ddl.auto">validate</property>
<!-- added as suggested in below answer-->
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>

<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

它抛出 org.hibernate.TransactionException: Unable to locate JTA UserTransaction

详细信息:

  • 我没有从任何应用程序服务器部署应用程序,而是将其作为计划 Java 应用程序运行。
  • 我正在使用 Hibernate 4.1

是否需要部署在应用服务器中?

我们是否必须在 Hibernate 4.1 版本中显式指定 JTAUsertransaction?

=============== 更新 ==============


hibernate.transaction.factory_class = org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory解决了这个错误,我就可以执行程序了。

但我还是想知道为什么它在使用 org.hibernate.transaction.JTATransactionFactory 时不起作用

最佳答案

使用时hibernate.transaction.factory_class = org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory

解决了这个错误,我可以执行程序了。

我特意将其发布为答案,以便对某人有所帮助。

关于java - 组织.hibernate.TransactionException : Unable to locate JTA UserTransaction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32091225/

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