[英]problems with Hibernate's integration in 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>
并且两个配置文件都在同一文件夹中。
如果您要提供磁盘上文件位置的绝对路径(例如c:/mapings/pool.hbm.xml),则它将不起作用,因为它会在类路径上搜索映射。 映射文件应位于jar或IDE类路径中。
如果您使用的是Tomcat + Web项目,则应在src文件夹中创建resource文件夹,并将映射文件放在那里,该文件等于:
<property name="mappingResources">
<list>
<value>object.hbm.xml</value>
</list>
</property>
希望能帮助到你。
采用
<property name="mappingResources" value="pool.hbm.xml" />
并将pool.hbm.xml
放在类路径的根目录中。 即您的bestofs.persistence.PoolBean
将位于类似<somewhere>/bestofs/persistence/PoolBean.class
的目录结构中。 映射文件应该在bestofs
旁边的<somewhere>
内部。
除非您发生了一些奇怪的ClassLoader魔术,否则这就是您需要做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.