使用ApplicationContext.xml配置hibernate时出错

时间:2013-07-30 07:42:13

标签: java spring hibernate java-ee configuration

我在使用Spring的propertiesPlaceholderConfigurer配置Hibernate时遇到了麻烦。我有一个文件db.properties,它包含带有db设置的键值对。现在我需要在Spring的applicationContext.xml文件中配置Hibernate。

但我一直收到错误,说:

javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:374)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ExceptionInInitializerError
    at com.dataart.mediaportal.db.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
    at com.dataart.mediaportal.db.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at com.dataart.mediaportal.dao.impl.UserDAOImpl.login(UserDAOImpl.java:45)
    at com.dataart.mediaportal.controller.bean.LoginBean.authorize(LoginBean.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 27 more
Caused by: org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
    at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
    at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
    at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:409)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:119)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2006)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1289)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
    at com.dataart.mediaportal.db.HibernateUtil.buildSessionFactory(HibernateUtil.java:12)
    ... 38 more

这是我的applicationContext.xml:

<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:osgi="http://www.springframework.org/schema/osgi"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
          http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
">

    <context:component-scan base-package="com.dataart.mediaportal" />

<!-- DATA SOURCE AND PERSISTENCE SETTINGS-->
    <bean id="propertiesPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dmDataSource"/>
        <property name="packagesToScan" value="com.dataart.mediaportal"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${db.dialect}</prop>
                <prop key="hibernate.show_sql">${db.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.import_files">${db.import_files}</prop>
                <prop key="hibernate.hbm2ddl.auto">${db.hbm2ddl_auto}</prop>
                <prop key="connection.pool_size">${db.pool_size}</prop>
                <prop key="current_session_context_class">${db.current_session_context_class}</prop>
                <prop key="hibernate.cache.provider_class">${db.provider_class}</prop>
                <prop key="hibernate.cache.use_second_level_cache">${db.use_second_level_cache}</prop>
                <prop key="hibernate.cache.use_query_cache">${db.use_query_cache}</prop>
            </props>
        </property>
    </bean>

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

    <bean id="dmDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
        <property name="maxActive" value="5" />
        <property name="maxWait" value="5000" />
    </bean>

</beans>

Folder's structure. Maybe this will clear the situation.

db.properies:

db.username=postgres
db.password=4351
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost/MediaPortalDB
db.pool_size=1
db.dialect=org.hibernate.dialect.PostgreSQLDialect
db.import_files=import.sql
db.hbm2ddl_auto=create
db.use_query_cache=true
db.use_second_level_cache=true
db.provider_class=org.hibernate.cache.HashtableCacheProvider
db.show_sql=true
db.current_session_context_class=thread

如果有人能指出解决方案或告诉我这里有什么问题,我真的很感激。

提前致谢。

纳扎尔。

3 个答案:

答案 0 :(得分:1)

由于错误的根本原因:

  

org.hibernate.HibernateException:Hibernate Dialect必须是显式的   设置

好像你没有正确定义属性文件中的dialect,它是强制属性。

您如何配置会话工厂(未在代码中显示)?您需要在调用configure()之前致电buildSessionFactory,例如:

加载默认的hibernate.cfg.xml:

new AnnotationConfiguration().configure().buildSessionFactory();

加载特定配置:

new AnnotationConfiguration().configure("hibernate-specific.cfg.xml")..buildSessionFactory();

答案 1 :(得分:1)

好像你错过了一个方言:

Caused by: org.hibernate.HibernateException: Hibernate Dialect must be explicitly set

添加这可能会有所帮助:

            <prop key="hibernate.dialect">
                org.hibernate.dialect.SQLServerDialect //add your specific dialect
            </prop>

这是hibernate属性之一。

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${db.dialect}</prop>
            <prop key="hibernate.show_sql">${db.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.import_files">${db.import_files}</prop>
            <prop key="hibernate.hbm2ddl.auto">${db.hbm2ddl_auto}</prop>
            <prop key="connection.pool_size">${db.pool_size}</prop>
            <prop key="current_session_context_class">${db.current_session_context_class}</prop>
            <prop key="hibernate.cache.provider_class">${db.provider_class}</prop>
            <prop key="hibernate.cache.use_second_level_cache">${db.use_second_level_cache}</prop>
            <prop key="hibernate.cache.use_query_cache">${db.use_query_cache}</prop>
        </props>
    </property>

将此信息放入您的媒体资源中:

db.dialect=org.hibernate.dialect.PostgreSQLDialect

如何加载属性文件:

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/connection.properties" />

会话工厂的一个例子:

<bean id="XXX" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      p:dataSource-ref="XXX" >
    <property name="annotatedClasses">
        <list>
            <value>XXX</value>
        </list>    
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                XXX
            </prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>

这是会议工厂声明的重要部分:

AnnotationSessionFactoryBean

答案 2 :(得分:0)

问题隐藏在另一个地方。我的应用程序(Facelets / Managed Beans)根本不是由Spring管理的。将下一行添加到web.xml后,所有工作都有效:

<application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
</application>

如果您正在使用Servlets / JSP - 请改为添加DispatcherServlet:

<servlet>
    <servlet-name>example</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>*.form</url-pattern>
</servlet-mapping>

希望有一天这会有所帮助。