Reputation: 3345
I am getting error java.lang.ClassNotFoundException: org.hibernate.cfg.AnnotationConfiguration while running a spring code from spring tool suite
I have already googled and checked stackoverflow for the resolution ,but no luck .
This is my pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.thoughtworkstest</groupId>
<artifactId>supermarket</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>supermarket</name>
<description>Demo project for Spring Boot</description>
<properties>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.156</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<!-- Dependencies for SQLite -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.25.2</version>
</dependency>
<dependency>
<groupId>net.kemitix</groupId>
<artifactId>sqlite-dialect</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
This is app-context.xml
<context:property-placeholder
location="classpath:META-INF/spring/hibernate.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses" value="com.test.market.model.ItemDetails" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
</beans>
Upvotes: 0
Views: 1075
Reputation: 125232
You are using Spring Boot 2.1.3 which depends on Spring 5.1. The Spring 5.x line works only with Hibernate 5.x, support for earlier versions has been dropped.
The spring-boot-starter-data-jpa
dependency already includes the hibernate-entitymanager
dependency in a version that works with the other dependencies.
Annother is is that you are mixing jars/modules from different versions of Spring you are mixing 5.1.x and 3.0.6, that is always trouble as those are quite incompatible. Furthermore that dependency is also included in the spring-boot-starter-data-jpa
.
So in short, you dependencies are a bit of mess.
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- Dependencies for SQLite -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.25.2</version>
</dependency>
<dependency>
<groupId>net.kemitix</groupId>
<artifactId>sqlite-dialect</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
NOTE: Spring Boot can also manage the H2 version, hence you can remove the version
tag from that.
You are using XML, however I would strongly suggest to ditch that and use JPA instead. With the current state of JPA there is no really a need to use plain Hibernate anymore. And if you really must, you can use EntityManager.unwrap(Session.class)
to obtain the underlying session.
The added advantage is that you can now use the auto-configuration for JPA from Spring Boot. The properties in your hibernate.properties
should go in the application.properties
under the correct names.
spring.datasource.url= # your jdbc.url value
spring.datasource.username = # your jdbc.username value
spring.datasource.password = # your jdbc.password value
spring.jpa.database-platform = # your hibernate.dialect value
spring.jpa.show-sql = # your hibernate.show_sql value
NOTE: You don't need to specify the driver-classname
property as Spring Boot and JDBC will detect that based on the given jdbc-url
.
TIP: Make sure that your @SpringBootApplication
annotated class is in the com.test.market
package, as recommended by the Spring Boot team, so it will detect all your classes, including your @Entity
annotated classes.
Your code, you might need to change to use EntityManager
instead of SessionFactory
and Session
but that should be a minor change.
Upvotes: 3
Reputation: 6226
Like @crizzis said try removing all other dependencies and provide only the following :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Spring starter data jpa already includes all the necessary dependencies like hibernate-core,hibernate-commons-annotations,javax.persistence-api,spring jdbc,h2 etc. So try removing all other data related dependencies from your pom and let springboot handle all such dependencies through it's starter.When you explicitly provide the version and dependency it will override the started dependency and may cause incompatible version issues if you are not careful.
Upvotes: 1