0
votes

I was trying to build a spring-mvc project without web.xml On starting the app on the server it threw the following exception on sessionFactory.

My AppConfig.class is as follows which is used to initialize the persistence unit:

package com.myapp;

import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc
@Configuration
@ComponentScan(basePackages="com.myapp")
@EnableTransactionManagement
public class AppConfig {

@Bean
public SessionFactory sessionFactory() {
    LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
    builder
        .scanPackages("com.myapp.model")
        .addProperties(getHibernateProperties());

    return builder.buildSessionFactory();
}

private Properties getHibernateProperties() {
    Properties prop = new Properties();
    prop.put("hibernate.format_sql", "true");
    prop.put("hibernate.show_sql", "true");
    prop.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    return prop;
}

@Bean(name = "dataSource")
public BasicDataSource dataSource() {

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://localhost:3306/eventtracker");
    ds.setUsername("root");
    ds.setPassword("root");
    return ds;
}

@Bean
public HibernateTransactionManager txManager() {
    return new HibernateTransactionManager(sessionFactory());
}

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new     InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}

And it is throwing exception :

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/myapp/AppConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: javax/transaction/SystemException

2
java.lang.NoClassDefFoundError: javax/transaction/SystemExceptionsidgate

2 Answers

0
votes

Take a look at your last exception.

nested exception is java.lang.NoClassDefFoundError: javax/transaction/SystemException

It means class file javax/transaction/SystemException is not on the classpath, thus jvm complain NoClassDefFoundError

Put all of your hibernate related jar on the classpath.

0
votes

It is a problem of Hibernate 5.0.6 release for Hibernate 5.0.3 everything works fine. To fix a problem add this to pom.xml (if you use 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