简体   繁体   中英

Spring MVC + Hibernate

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

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 ?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM