2012-12-18 39 views
1

我有一个使用Spring3,Hibernate4和JSF2的小应用程序。我使用Spring applicationContext.xml来扫描Entity类的类和注释。hibernate.cfg.xml和applicationContext.xml

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <context:component-scan base-package="net.test" /> 
    <!-- Data Source Declaration --> 
    <bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
     destroy-method="close"> 
     <property name="driverClass" value="oracle.jdbc" /> 
     <property name="jdbcUrl" 
      value="jdbc:oracle:thin:@server:1521:TEST" /> 
     <property name="user" value="scott" /> 
     <property name="password" value="tiger" /> 
     <property name="maxPoolSize" value="10" /> 
     <property name="maxStatements" value="0" /> 
     <property name="minPoolSize" value="5" /> 
    </bean> 

    <!-- Session Factory Declaration --> 
    <bean id="SessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="DataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>net.test.model.Employees</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props>    
     </property> 
    </bean> 
    <!-- Enable the configuration of transactional behavior based on annotations --> 
    <tx:annotation-driven transaction-manager="txManager" /> 
    <!-- Transaction Manager is defined --> 
    <bean id="txManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="SessionFactory" /> 
    </bean> 
</beans> 

我想这样做的就是添加hibernate.cfg.xml到我的应用程序,我知道这是不是强制性的,如果我有applicaionContext.xml存在。

我为什么要包括的原因是因为我想指定hibernate.cfg.xml为了解决

org.hibernate.QueryException: ClassNotFoundException: 
    org.hibernate.hql.internal.ast.HqlToken 

问题见this例外的细节我正在以下

<property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"> 
</property> 

由于我在applicationContext.xml中已经有了以下内容,所以我最好使用hibernate.cfg.xml?任何帮助是非常可观的。

<property name="annotatedClasses"> 
      <list> 
       <value>net.test.model.Request</value> 
      </list> 
     </property> 

更新1

Error creating bean with name 'requestDAOImpl': Injection of autowired dependencies 
failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private org.hibernate.SessionFactory 
net.test.request.dao.RequestDAOImpl.sessionFactory; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'SessionFactory' defined in ServletContext resource 
[/WEB-INF/applicationContext.xml]: Invocation of init method failed; 
nested exception is org.hibernate.HibernateException: could not 
    instantiate QueryTranslatorFactory: 
    org.hibernate.hql.classic.ClassicQueryTranslatorFactory 
. 

回答

1

您是否尝试过使用Spring的application.xml中这样?:

<bean id="SessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     ... 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</prop> 
      </props>    
     </property> 
    </bean> 

这似乎设置* hibernate.query.factory_class *属性我认为这应该没有显式使用hibernate.cfg.xml工作。

+0

我在applicationContext.xml中添加了org.hibernate.hql.classic.ClassicQueryTranslatorFactory。但是,我部署时几乎没有例外。我通过编辑我的问题来添加例外作为更新1。 – user75ponic

+0

哦,是的,这工作,非常感谢。对于Hibernate4,它应该像'org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory'。一旦你节约了我的一天,自下午以来一直试图解决这个问题。赞赏。 – user75ponic

+1

在我看来,在ClassicQueryTranslatorFactory位于org.hibernate.hql.internal.classic上的位置更改了软件包。查看我的更新。 – dimas