0

我在使用 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>

文件夹的结构。 也许这会清除情况。

数据库属性:

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

如果有人能指出解决方案或告诉我这里可能出了什么问题,我将不胜感激。

提前致谢。

纳扎尔。

4

3 回答 3

1

由于错误的根本原因:

org.hibernate.HibernateException:必须显式设置休眠方言

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

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

要加载默认 hibernate.cfg.xml:

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

要加载特定配置:

new AnnotationConfiguration().configure("hibernate-specific.cfg.xml")..buildSessionFactory();
于 2013-07-30T07:45:32.643 回答
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>

这是休眠属性之一。

    <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
于 2013-07-30T07:47:12.223 回答
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>

希望有一天这会对某人有所帮助。

于 2013-07-31T17:31:21.603 回答