为什么Hibernate 5.0.6 发布包中不包含事务实现jar?

Why Hibernate 5.0.6 release package does not contain transaction implementation jar?

我有这样的代码:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class Main {

    public static void main(String[] args) {
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure().build();
        SessionFactory sessionFactory = null;
        try {
            sessionFactory = new MetadataSources(registry).buildMetadata()
                    .buildSessionFactory();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(registry);
        }

        if (sessionFactory != null) {
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setRollNo(1);
            studentInfo.setName("Dmytro");

            Session session = sessionFactory.openSession();
            session.beginTransaction();

            session.save(studentInfo);

            session.getTransaction().commit();
            session.close();
            sessionFactory.close();
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }
}

抛出异常:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:274)

我读到我应该将所需文件夹中的事务 API jar 添加到 claspath。但是,Hibernate 5.0.6 发布包不包含它。

我应该手动添加事务 API 实现吗?

要使用 Hibernate Transaction API,您需要从会话中获取 org.hibernate.Transaction。 看到这个 link : https://docs.jboss.org/hibernate/orm/5.0/userGuide/en-US/html_single/#transactions-api

看起来是Hibernate 5.0.6 版本的问题。对于 Hibernate 5.0.3 版本,无需手动添加 transaction-api-1.1.jar

添加所需的 jar

对于 Maven

<dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
</dependency>

用于Gradle构建

compile group: 'javax.transaction', name: 'transaction-api', version: '1.1'

手动下载

http://central.maven.org/maven2/javax/transaction/transaction-api/1.1/transaction-api-1.1.jar

更新

这不是错误,而是功能。

Due to the proliferation of JTA artifacts under various GAVs, Hibernate exporting one as a transitive dependency can cause problems in environments where another GAV is used. This should be marked as provided to make sure people don't get it on classpath without explicitly asking for it.

从那里开始

Make JTA a provided (non-transitive) dependency

更新 2

无论如何这是一个错误。 Hibernate 5.0.7 的依赖中不需要提供transaction-api-1.1.jar

Hibernate exposes javax.transaction.Synchronization from a public API. Whether you use JTA or not, you can register a JTA Synchronization for transaction callbacks. So the JTA spec jar is a non-optional dependency. Allowing this to become non-transitive was a mistake which will be reverted: see HHH-10307

我同意v.ladynev的回答。 5.0.3 版包含事务 API 实现 jar,但是 5.0.6 版没有。

来自 maven 存储库的屏幕截图

核心 Hibernate O/RM 功能 » 5.0.3.Final 依赖项:

核心 Hibernate O/RM 功能 » 5.0.6.Final 依赖项:

似乎在某些应用程序中没有使用事务API,所以如果需要可以手动添加它。