org.hibernate.TransactionException:无法找到JTA UserTransaction

时间:2015-08-19 09:12:31

标签: java hibernate

我正在编写第一个使用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:无法找到JTA UserTransaction

详细信息:

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

是否有必要在Application Server中部署它?

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

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

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

但是我仍然想知道使用org.hibernate.transaction.JTATransactionFactory

时它无效的原因

2 个答案:

答案 0 :(得分:2)

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

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

我故意将其作为答案发布,因此对某人有帮助。

答案 1 :(得分:0)

您将缺少hibernate配置文件中的JTA配置

<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>