2011-09-18 89 views
0

我有一個簡單的Java應用程序,我試圖在Spring中集成Hibernate,但似乎Spring配置文件找不到* .hbm.xml(映射資源): 我有一個文件名爲持久性context.xml中,我使用它作爲一個Spring配置文件,我有以下豆聲明:Hibernate在Spring中集成的問題

org.hibernate.dialect.MySQLDialect

但是被拋出的異常: java.io.FileNotFoundException:類路徑資源[pool.hbm.xml]無法打開,因爲它不存在 我甚至嘗試給映射資源屬性一個絕對路徑值。它不起作用。 謝謝!

UPDATE: 我的春天的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> 

而且兩者的配置文件在同一文件夾中。

+4

請張貼您的SPring conf xml文件,特別是Hibernate配置。 –

+0

我更新了我的文章 – spauny

回答

1

如果給出磁盤上文件位置的絕對路徑(例如c:/mapings/pool.hbm.xml),它將無法工作,因爲它搜索類路徑上的映射。映射文件應位於您的jar或IDE類路徑中。

+0

但是,如果它不是一個Web應用程序,我應該在哪裏放置這個配置文件?在同一個文件中的類? – spauny

+0

是的,它應該和你的課程在同一個文件夾中。 –

0

如果您使用的是Tomcat + web項目,你應該你的src文件夾中創建資源文件夾,並把你的映射文件有它等於:

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

希望它能幫助。

0

使用

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

,並把pool.hbm.xml你的classpath的根。即您的bestofs.persistence.PoolBean將位於目錄結構中,如<somewhere>/bestofs/persistence/PoolBean.class。映射文件應位於<somewhere>之內,與bestofs並排。

這就是你需要做的,除非你有一些奇怪的ClassLoader魔術發生。