Hibernate在Spring中集成的问题

时间:2011-09-18 08:21:06

标签: java hibernate spring

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

                                    org.hibernate.dialect.MySQLDialect              

但是被抛出异常: 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>

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

希望它有所帮助。

答案 2 :(得分:0)

使用

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

并将pool.hbm.xml放在类路径的根目录中。即您的bestofs.persistence.PoolBean将位于<somewhere>/bestofs/persistence/PoolBean.class之类的目录结构中。映射文件应位于<somewhere>内,与bestofs一起。

除非你有一些奇怪的ClassLoader魔法,否则你需要做的就是。