2015-04-30 40 views
0

我创建了一个maven web项目。该堆栈是Spring + Hibernate +球衣。hibernate.cfg.xml未被拾取

我在src/main/resources/path下有这个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> 

我如何使这项工作?

谢谢。

+0

我的意图是自动拾取hibernate.cfg.xml。我不想以编程的方式明确地调用它。 – Aspirant9

+0

什么是您的项目中的hibernate.cfg.xml文件的位置? – RE350

回答

3

根据日志,正在尝试配置HibernateLocalSessionFactoryBean

同样在你的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> 

你是刚刚设置休眠属性dialecthibernate.default_schema

要解决该问题,请将属性dialect更改为hibernate.dialect

现在,如果您仍然想使用Hibernate配置文件,那么您需要在弹簧配置文件中设置configLocation属性。

下面是关于如何使用configLocation属性的示例:

<bean id="WorkspacesDBSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
    </bean> 

你可以看到详细的解释在这个职位: applicationContext.xml with datasource or hibernate.cfg.xml. Difference?

0

你的hibernate.cfg.xml必须在CLASSPATH挑自动启动。尝试把/ src/main/java。 由于Hibernate首先在CLASSPATH中发现默认(/ src/main/java)

+0

这是一个maven项目,如这个问题的标签所示,所以将它保存在src/main/resources中是将配置文件放在maven中的正确方法。一旦构建项目,配置文件将被放置在类路径的根目录中。 – Chaitanya

+0

但是,当他运行maven时,它如何配置maven的配置? –

+0

我不明白你的问题,请你详细说明一下吗? – Chaitanya