Java org.hibernate.TransactionException:找不到JTA UserTransaction

Java org.hibernate.TransactionException:找不到JTA UserTransaction,java,hibernate,Java,Hibernate,我正在编写使用Hibernate框架插入记录的第一个示例 首先,我正试图按照 你可以在GIT上看到我的代码 当调用下面的代码时 public Long saveEmployee(Employee emp){ Session session = new Configuration().configure().buildSessionFactory().openSession(); session.beginTransaction(); Long id = (Long) se

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

首先,我正试图按照

你可以在GIT上看到我的代码

当调用下面的代码时

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>

com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/test
根
根123
2.
org.hibernate.dialogue.mysqldialogue
线
org.hibernate.cache.NoCacheProvider
假的
验证
org.hibernate.transaction.JTATransactionFactory
它抛出org.hibernate.TransactionException:无法找到JTA UserTransaction

详情:

  • 我不是从任何应用服务器部署应用程序,而是作为PlanJava应用程序运行它
  • 我正在使用Hibernate4.1
是否有必要在应用服务器中部署它

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

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


hibernate.transaction.factory\u class=org.hibernate.engine.transaction.internal.jdbc.jdbc TransactionFactory
解决了这个错误,我可以执行程序


但是我仍然想知道为什么在使用
org.hibernate.transaction.JTATransactionFactory

时它不起作用,因为您的hibernate配置文件中缺少JTA配置

<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>  
org.hibernate.transaction.JTATransactionFactory
使用时
hibernate.transaction.factory\u class=org.hibernate.engine.transaction.internal.jdbc.jdbc TransactionFactory

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


我故意把它作为一个答案贴出来,这样对别人会有帮助。

还是有同样的例外
org.hibernate.TransactionException:找不到JTA UserTransaction
请发布配置(cfg)文件好吗?