4

I have a code like that:

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);
        }
    }
}

It throws an exception:

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)

I read that I should add transaction API jar from required folder to the claspath. However, Hibernate 5.0.6 release package does not contain it. enter image description here

Should I add transaction API implementation manually?

Dmytro Plekhotkin
  • 1,965
  • 2
  • 23
  • 47

3 Answers3

2

Looks like it is a problem of Hibernate 5.0.6 release. There is no need to manually add transaction-api-1.1.jar for Hibernate 5.0.3 release.

To add required jar

for Maven

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

for Gradle build

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

manual download

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

Update

It is not a bug, it is a feature.

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.

From there

Make JTA a provided (non-transitive) dependency

Update 2

It was a bug anyway. It is not need to provide transaction-api-1.1.jar in the dependences for Hibernate 5.0.7.

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
  • 19,275
  • 8
  • 46
  • 67
0

To use Hibernate Transaction API, you would obtain the org.hibernate.Transaction from the Session. See this link : https://docs.jboss.org/hibernate/orm/5.0/userGuide/en-US/html_single/#transactions-api

  • I included full Java class code example. As you see I begin transaction and close it, however Java throws an error earlier in the code. – Dmytro Plekhotkin Dec 28 '15 at 11:27
0

I agree with v.ladynev answer. Release 5.0.3 contains transaction API implementation jar, however, release 5.0.6 does not.

Screenshots from maven repository

Core Hibernate O/RM Functionality » 5.0.3.Final dependencies: enter image description here

Core Hibernate O/RM Functionality » 5.0.6.Final dependencies: enter image description here

It seems that transaction API is not used in some applications, so if you need it you can add it manually.

Dmytro Plekhotkin
  • 1,965
  • 2
  • 23
  • 47