[英]hibernate.cfg.xml not being picked up
我已經創建了一個Maven Web項目。 堆棧是春季+冬眠+球衣。
我在src / main / resources /路徑下有這個hibernate.cfg.xml。 當我嘗試將它部署到tomcat時,我得到了這個堆棧跟蹤,
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'WorkspacesDBSessionFactory' defined in class path resource [ApplicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:917)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:860)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:775)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
... 24 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:422)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:863)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:782)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
... 34 more
當我檢查DialectFactory.java:57時,我找到了代碼,
public static Dialect determineDialect(String databaseName, int databaseMajorVersion) {
if ( databaseName == null ) {
throw new HibernateException( "Hibernate Dialect must be explicitly set" );
}
因此,實際的問題是未加載hibernate.cfg.xml。 因此,我刪除了文件並嘗試部署。 我又得到了相同的堆棧跟蹤。
我的ApplicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-3.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
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="org.work.autogen, org.work.db.api, org.work.services, org.work.routes">
</context:component-scan>
<bean id="log4jInitializer"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>classpath:log4j.properties</value>
</list>
</property>
</bean>
<bean id="WorkspacesDBSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- <property name="mappingResources">
<list>
<value>Appraisal.hbm.xml</value>
<value>Managers.hbm.xml</value>
<value>EmployeeSecretUuid.hbm.xml</value>
<value>Work.hbm.xml</value>
<value>Company.hbm.xml</value>
<value>Employee.hbm.xml</value>
</list>
</property> -->
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.default_schema">Workspacedb</prop>
</props>
</property>
</bean>
</beans>
我的Hibernate.cfg.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><!-- name="sessionFactory"> -->
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Workspacedb</property>
<property name="hibernate.connection.username">workadmin</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="Appraisal.hbm.xml" />
<mapping resource="Managers.hbm.xml" />
<mapping resource="EmployeeSecretUuid.hbm.xml" />
<mapping resource="Work.hbm.xml" />
<mapping resource="Company.hbm.xml" />
<mapping resource="Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
我該如何做這項工作?
謝謝。
根據日志,正是LocalSessionFactoryBean
試圖配置Hibernate
。
此外,在Spring配置文件中,您沒有提到擁有單獨的Hibernate配置文件的任何地方。
所以在這段代碼中:
<bean id="WorkspacesDBSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.default_schema">Workspacedb</prop>
</props>
</property>
</bean>
您只是為dialect
和hibernate.default_schema
設置了休眠屬性。
要解決此問題,請將屬性dialect
更改為hibernate.dialect
。
現在,如果您仍然想使用Hibernate配置文件,則需要在spring配置文件中設置configLocation屬性。
這是有關如何使用configLocation屬性的示例:
<bean id="WorkspacesDBSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
您可以在這篇文章中找到詳細的解釋: 帶有數據源或hibernate.cfg.xml的applicationContext.xml。 區別?
您的hibernate.cfg.xml必須位於CLASSPATH中才能自動提取。 嘗試放入/ src / main / java。 因為Hibernate首先在CLASSPATH中找到default(/ src / main / java)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.