0

i've a problem with hibernate configuration in Spring. My project is a repository project. This is 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>

<groupId>Framework-core-data-oracle</groupId>
<artifactId>Framework-core-data-oracle</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.2.8.Final</version>
    </dependency>
</dependencies>

This are my beans definition:

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>

<bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

This is my hibernate config xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@10.10.10.4:1521:BAGSTORAGE</property>
    <property name="hibernate.connection.username">system</property>
    <property name="hibernate.connection.password">bagstorage!</property>
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="hibernate.default_schema">BAGSTORAGE</property>
    <property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>

When i try to autowire SessionFactory i've got this error:

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate4.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource -> nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean

I have spring orm added in pom, spring mvc version is > 3, so there is no problem with hibernate 4. Any ideas?

I've this situation: Project A: core project, with spring dependency in maven Project B: Repository project, where there are core project (project A), hibernate-core, oracle and spring-orm dependencies Project C: Work project, with beans configurations and project A and project B in dependencies

Thanks

2 Answers2

1

Your configuration looks correct, but I see you depend on spring-core 3.1.1 and spring-orm 4.0.0. You really should align the versions of all your Spring modules. Please try to upgrade spring-core to version 4.0.0 and see if the error goes away ?

Olivier Croisier
  • 6,139
  • 25
  • 34
0

Usually, when a class is not loaded/found, there are 2 reasons:

1. There are multiple versions of it in the classpath(2+ artifacts containing it)
2. There is no class defined in the classpath

You have different versions for Spring modules, it is usually a best practice to keep them in sync, like this:

<properties>
  <spring.version>4.0.0.RELEASE</spring.version>
</properties>

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
  </dependency>
</dependencies>
Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
  • Thanks for the answer, but i've tried with same version of spring and spring-orm, and all reference are specified only one time. I've always the same error. I've this situation: Project A: core project, with spring dependency in maven Project B: Repository project, where there are core project (project A), hibernate-core, oracle and spring-orm dependencies Project C: Work project, with beans configurations and project A and project B in dependencies – user2992626 Jan 14 '14 at 09:37
  • @user2992626 make sure that you don't have a transitive dependency that duplicates a dependency. I believe that you posted the B or C's POM, maybe you get something from project A. Try to comment out all the dependencies in these projects and rely on A's dependencies and and add them one by one until it's back compiling and running. You will be sure that's not a classpath problem at least. – Silviu Burcea Jan 14 '14 at 11:38