我有这样的代码:
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 实现吗?