hibernate.cfg.xml和applicationContext.xml

时间:2012-12-18 14:29:29

标签: java spring hibernate

我有一个使用Spring3,Hibernate4和JSF2的小应用程序。

到目前为止,在我的应用程序中,我没有hibernate.cfg.xml文件,我使用Spring 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 个答案:

答案 0 :(得分:1)

您是否尝试使用Spring的application.xml设置* hibernate.query.factory_class *属性?:

<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.cfg.xml的情况下工作。