【问题标题】:What is wrong with my Spring and Hibernate configuration?我的 Spring 和 Hibernate 配置有什么问题?
【发布时间】:2011-09-14 09:24:53
【问题描述】:

这个配置有什么问题?我正在使用 Spring 3 和 Hibernate 3.6 并尝试设置为使用 DAO。我在 hibernate.cfg.xml 中看到两个错误。

第一个在 session-factory 标签上-

 The content of element type "session-factory" must match 
"(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)".

第二个是关于-property name="dataSource"-tag-

The content of element type "property" must match "null".

应用程序上下文.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <bean id="dataSource" 
    class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
      <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
      </property>
      <property name="url">
        <value>jdbc:mysql://localhost/projectName</value>
      </property>
      <property name="username">
        <value>uname</value>
      </property>
      <property name="password">
        <value>pass</value>
      </property>
      <!-- Disable the second-level cache  -->
        <!-- Echo all executed SQL to stdout -->
    </bean>
    </beans>

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
       <ref bean="dataSource"/>
    </property>
    </bean>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <property name="transaction.factory_class">
    org.hibernate.transaction.JDBCTransactionFactory
    </property>
    <property name="current_session_context_class">
    thread
    </property>
    <mapping class="com.projectname.model.name"/>
    <mapping class="com.projectname.model.stuff"/> 
</session-factory>
</hibernate-configuration>

【问题讨论】:

    标签: xml hibernate spring hibernate3


    【解决方案1】:

    您混合了 Hibernate 和 Spring 配置。它们是完全分开的。您不能在 Hibernate 配置中使用 Spring bean。

    这将需要转到您的 Spring 配置:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
       <ref bean="dataSource"/>
    </property>    
      <!-- SQL dialect -->
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <!-- Disable the second-level cache  -->
      <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
      <!-- Echo all executed SQL to stdout -->
      <property name="show_sql">true</property>
      <property name="transaction.factory_class">
       org.hibernate.transaction.JDBCTransactionFactory
      </property>
      <property name="current_session_context_class">
      thread
      </property>
    </bean>
    

    由于我通常使用注释配置,因此我可能会在小事上误会,但您应该返回文档。

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 2011-05-19
      • 2011-05-15
      • 2012-11-06
      • 1970-01-01
      • 2017-06-14
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多