编程语言
首页 > 编程语言> > java – 为什么Hibernate 5.0.6发布包不包含事务实现jar?

java – 为什么Hibernate 5.0.6发布包不包含事务实现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发行包不包含它.
enter image description here

我应该手动添加事务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

更新

它不是一个bug,它是一个功能.

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 07002

标签:java,hibernate,hibernate-5-x
来源: https://codeday.me/bug/20190702/1356391.html