【问题标题】:problems with Hibernate's integration in SpringHibernate 在 Spring 中的集成问题
【发布时间】:2011-09-18 08:21:06
【问题描述】:

我有一个简单的 Java 应用程序,我正在尝试在 Spring 中集成 Hibernate,但似乎 Spring 配置文件找不到 *.hbm.xml(映射资源): 我有一个名为 persistence-context.xml 的文件,我将它用作 Spring 配置文件,并且声明了以下 bean:

org.hibernate.dialect.MySQL方言

但是正在抛出异常: java.io.FileNotFoundException:类路径资源 [pool.hbm.xml] 无法打开,因为它不存在 我什至尝试给映射资源属性一个绝对路径值。它不起作用。 谢谢!

更新: 我的 Spring conf 文件:

    <?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/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">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value='jdbc:mysql://localhost/bestofs_seinfeld' />
        <property name="username" value="root" />
        <property name="password" value="futifuti825300" />
        <property name="initialSize" value="5" />
        <property name="maxActive" value="10" />
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources" value="pool.hbm.xml" />
        <property name="hibernateProperties">
            <props>
                <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
            <ref bean="mySessionFactory"/>
        </property>
    </bean>

    <bean id="voteDao" class="bestofs.persistence.HibernatePoolDao">
    <property name="hibernateTemplate">
        <ref bean="hibernateTemplate"/>    
    </property>
</bean>
</beans>

而我的 pool.hbm.xml 是:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="bestofs.persistence.PoolBean" table="sein_pool">
    <id name="idVote" column="ID_Vote">
        <generator class="assigned"/>
    </id>

    <property name="IdActor">
        <column name="ID_Actor"/>
    </property>
    <property name="IdUser">
        <column name="ID_User"/>
    </property>
    <property name="IdSession">
        <column name="ID_Session"/>
    </property>
</class>
</hibernate-mapping>

并且两个配置文件都在同一个文件夹中。

【问题讨论】:

  • 请发布您的 SPring conf xml 文件,尤其是 Hibernate 配置。

标签: java hibernate spring


【解决方案1】:

如果您提供磁盘上文件位置的绝对路径(例如 c:/mapings/pool.hbm.xml),它将不起作用,因为它会在类路径上搜索映射。映射文件应该在您的 jar 或 IDE 类路径中。

【讨论】:

  • 但是如果它不是一个网络应用程序,我应该把这个配置文件放在哪里?与类在同一个文件中?
  • 是的,它应该与您的课程在同一个文件夹中。
【解决方案2】:

如果你使用的是 Tomcat + web 项目,你应该在你的 src 文件夹中创建资源文件夹并将你的映射文件放在那里它等于:

 <property name="mappingResources">
        <list>
          <value>object.hbm.xml</value>
        </list>
    </property> 

希望对你有帮助。

【讨论】:

    【解决方案3】:

    使用

    <property name="mappingResources" value="pool.hbm.xml" />
    

    并将pool.hbm.xml 放在类路径的根目录中。 IE。您的bestofs.persistence.PoolBean 将位于类似&lt;somewhere&gt;/bestofs/persistence/PoolBean.class 的目录结构中。映射文件应位于&lt;somewhere&gt; 内,紧邻bestofs

    这就是你需要做的所有事情,除非你发生了一些奇怪的 ClassLoader 魔法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2013-12-10
      • 2012-01-01
      • 2013-02-26
      相关资源
      最近更新 更多