java – 为什么Hibernate 5.0.6发布包不包含事务实现jar?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 为什么Hibernate 5.0.6发布包不包含事务实现jar?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2474字,纯文字阅读大概需要4分钟。
内容图文
![java – 为什么Hibernate 5.0.6发布包不包含事务实现jar?](/upload/InfoBanner/zyjiaocheng/780/57a3d48a0ad847288c60cf11ec95ce67.jpg)
我有这样的代码:
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 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 5.0.6发布包不包含事务实现jar?全部内容,希望文章能够帮你解决java – 为什么Hibernate 5.0.6发布包不包含事务实现jar?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。