1
votes

I'm trying out a spring-boot-starter project. And trying out a simple project with jpa with a POSTGRES db behind it. However when launching the app I get a ClassNotFoundException:

Caused by: java.lang.ClassNotFoundException: org.hibernate.boot.model.naming.ImplicitNamingStrategy

But how is that possible? I thought the spring-boot-starter-jpa would dependency in maven would take care of all the required dependencies...

@Configuration
@PropertySource("classpath:application.properties")
public class AuthConfiguration {
    @Value("${hibernate.driver_class}")
    private String DATABASE_DRIVER_CLASS;

    @Value("${hibernate.url}")
    private String DATABASE_URL;

    @Value("${hibernate.username}")
    private String DATABASE_USER;

    @Value("${hibernate.password}")
    private String DATABASE_PASSWORD;

    @Value("${hibernate.dialect}")
    private String HIBERNATE_DIALECT;

    @Value("${hibernate.packages_to_scan}")
    private String HIBERNATE_PACKAGES_TO_SCAN;

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(DATABASE_DRIVER_CLASS);
        dataSource.setUrl(DATABASE_URL);
        dataSource.setUsername(DATABASE_USER);
        dataSource.setPassword(DATABASE_PASSWORD);
        return dataSource;
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        final LocalSessionFactoryBean localSessionFactoryBean =
                new LocalSessionFactoryBean();
        localSessionFactoryBean.setDataSource(dataSource());
        localSessionFactoryBean.setPackagesToScan(HIBERNATE_PACKAGES_TO_SCAN);
        {
            final Properties properties = new Properties();
            properties.put("hibernate.dialect", HIBERNATE_DIALECT);
            localSessionFactoryBean.setHibernateProperties(properties);
        }
        return localSessionFactoryBean;
    }
}

The properties I am using

hibernate.driver_class = org.postgresql.Driver
hibernate.datasource_class = org.postgresql.ds.PGSimpleDataSource
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
hibernate.url = jdbc:postgresql://localhost:5432/test
hibernate.username = postgres
hibernate.password = password
hibernate.show_sql=false
hibernate.generate_statistics=false
hibernate.packages_to_scan=com.test.model

Here is my maven setup

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>auth</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>auth</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1201-jdbc41</version>
        </dependency>
        <!--
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.6.0</version>
        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

I don't know how to fix this. Adding hibernate-core dependency itself didn't solve it either as I read on another stackoverflow post, but it feels bad as well to do so because everything should be in the starter dependency...

Any ideas what I am missing?

1
Which Hibernate version you have in the class path with those dependencies? – v.ladynev
As a sidenote, Spring Boot takes care of everything you're doing in your @Configuration class. You don't need to configure Hibernate yourself, just use Spring data abstractions: docs.spring.io/spring-data/jpa/docs/current/reference/html and use the Spring Boot given application properties instead of configuring the ones for Hibernate yourself. Just follow the Spring Boot: Accessing Data with JPA tutorial: spring.io/guides/gs/accessing-data-jpa – Xtreme Biker

1 Answers

1
votes

Ok I found the problem. Apparantly I was using a class that comes from a package ...hibernate5... while only the latest stable version was included by the spring-boot-starter-jpa dependency. So as a result it could not find any hibernate5 classes that it required.

Using ...hibernate4... instead in my imports fixed the problem.

For completion this was my dependency tree:

[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.3.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.5:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.5:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.16:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.16:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.16:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:4.2.5.RELEASE:compile
[INFO] |  |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.8:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat:tomcat-jdbc:jar:8.0.32:compile
[INFO] |  |  |  \- org.apache.tomcat:tomcat-juli:jar:8.0.32:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:4.2.5.RELEASE:compile
[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile

Need to use this one:

**[INFO] |  |  +- org.hibernate:hibernate-core:jar:4.3.11.Final:compile**

While I was using for example this orm class:

org.springframework.orm.hibernate**5**.LocalSessionFactoryBean

Rest of the dependency tree:

[INFO] |  |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |  \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:1.9.4.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:1.11.4.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-orm:jar:4.2.5.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-context:jar:4.2.5.RELEASE:compile
[INFO] |  |  |  \- org.springframework:spring-expression:jar:4.2.5.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-tx:jar:4.2.5.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:4.2.5.RELEASE:compile
[INFO] |  |  +- org.slf4j:slf4j-api:jar:1.7.16:compile
[INFO] |  |  \- org.slf4j:jcl-over-slf4j:jar:1.7.16:compile
[INFO] |  \- org.springframework:spring-aspects:jar:4.2.5.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-rest:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-web:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.3.RELEASE:compile
[INFO] |  |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.32:compile
[INFO] |  |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.32:compile
[INFO] |  |  |  +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.32:compile
[INFO] |  |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.32:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-validation:jar:1.3.3.RELEASE:compile
[INFO] |  |  |  \- org.hibernate:hibernate-validator:jar:5.2.4.Final:compile
[INFO] |  |  |     +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  |     \- com.fasterxml:classmate:jar:1.1.0:compile
[INFO] |  |  +- org.springframework:spring-web:jar:4.2.5.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-webmvc:jar:4.2.5.RELEASE:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.5:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.5:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.6.5:compile
[INFO] |  \- org.springframework.data:spring-data-rest-webmvc:jar:2.4.4.RELEASE:compile
[INFO] |     +- org.springframework.data:spring-data-rest-core:jar:2.4.4.RELEASE:compile
[INFO] |     |  +- org.springframework.hateoas:spring-hateoas:jar:0.19.0.RELEASE:compile
[INFO] |     |  +- org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile
[INFO] |     |  \- org.atteo:evo-inflector:jar:1.2.1:compile
[INFO] |     \- com.github.fge:json-patch:jar:1.7:compile
[INFO] |        +- com.github.fge:jackson-coreutils:jar:1.6:compile
[INFO] |        |  +- com.github.fge:msg-simple:jar:1.1:compile
[INFO] |        |  |  \- com.github.fge:btf:jar:1.2:compile
[INFO] |        |  \- com.google.guava:guava:jar:16.0.1:compile
[INFO] |        \- com.google.code.findbugs:jsr305:jar:2.0.1:compile