Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java EE (J2EE) и Spring > Hibernate+Spring не инициализируется


Автор: Flap 5.8.2011, 12:35
Доброго времени суток. Помогите решить проблему.
В стандартном коде HibernateUtil не работает строка "sessionFactory = new Configuration().configure().buildSessionFactory();"

Код

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
     
public class HibernateUtil {
    private static final SessionFactory sessionFactory;
 
    static {
        try {
            //creates the session factory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (ExceptionInInitializerError ex) {
            System.err.println("Initial SessionFactory creation failed: " + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
 
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}


Конструктор контроллера (специально вынес сюда, чтобы легче было отлаживать):
Код

public class IndexController extends SimpleFormController{

    private Session session;

    public IndexController() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
    }
}


Выдает ошибку:
Цитата

04.08.2011 9:15:48 org.springframework.web.servlet.FrameworkServlet initServletBean
SEVERE: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController' defined in file [C:\javaDev\tomcat6\webapps\MyBlog\WEB-INF\classes\myblog\web\controller\IndexController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [myblog.web.controller.IndexController]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [myblog.web.controller.IndexController]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError

Caused by: java.lang.ClassCastException: org.hibernate.annotations.common.reflection.java.JavaReflectionManager cannot be cast to org.hibernate.annotations.common.reflection.MetadataProviderInjector


Смысл ошибки я понимаю, но где что нужно вписать - понятия не имею, только осваиваю это направление.

Подскажите, как это лечится.

web.xml:
Код

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MyBlog</display-name>
  
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>


dispatcher-servlet.xml :
Код

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    <context:component-scan base-package="myblog.web.controller" />
    <context:component-scan base-package="myblog.service"/>    
    <context:component-scan base-package="myblog.dao"/>
    
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

</beans>


hibernate.cfg.xml:
Код

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost/db_myblog
        </property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.autocommit">true</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <!-- Mapping files -->
        <mapping resource="myblog.entity.Post"/>
    </session-factory>
</hibernate-configuration>

Спасибо.

Автор: Stolzen 5.8.2011, 13:01
Зачем вам hibernate.cfg и HibernateUtil, если вы используете спринг? 
Вот взгляните на пример: https://raw.github.com/jtalks-org/jtalks/swallows.develop/poulpe/poulpe-model/src/main/resources/org/jtalks/poulpe/model/entity/applicationContext-dao.xml

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)