首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >applicationcontext.xml和.hbm文件放在哪里?

applicationcontext.xml和.hbm文件放在哪里?
EN

Stack Overflow用户
提问于 2012-05-24 16:46:51
回答 2查看 6.9K关注 0票数 3

我正在学习spring堆栈,并且在执行本教程之后的第一个crud时,我将applicationContext.xml放入webapp/webapp中,并将.hbm.xml放入到资源/映射中,但我不知道为什么hbm文件一直显示无法找到我的pojos。

在github https://github.com/kossel/firstzk

我有这个结构

applicationContext.xml

代码语言:javascript
复制
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- set other Hibernate properties in hibernate.cfg.xml file -->
        <property name="configLocation" value="classpath:/com/iknition/firstzk/hibernate/hibernate.cfg.xml" />
    </bean>

hibernate.cfg.xml

代码语言:javascript
复制
    <mapping resource="com/iknition/firstzk/hibernate/Company.hbm.xml" />
    <mapping resource="com/iknition/firstzk/hibernate/Contact.hbm.xml" /> 

Company.hbm.xml

代码语言:javascript
复制
<?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 package="com.iknition.firstzk.beans">
    <class name="Contact" table="contact">
        <id name="idcontact" column="idcontact" type="integer">
            <generator class="increment" />
        </id>
        <property name="name" column="name" type="string" />
        <property name="email" column="email" type="string" />
        <many-to-one name="company" column="companyId" class="com.iknition.firstzk.beans.Company" outer-join="true" />
    </class>
</hibernate-mapping>

Contact.hbm.xml:

代码语言:javascript
复制
<?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 package="com.iknition.firstzk.beans">
    <class name="Contact" table="contact">
        <id name="idcontact" column="idcontact" type="integer">
            <generator class="increment" />
        </id>
        <property name="name" column="name" type="string" />
        <property name="email" column="email" type="string" />
        <many-to-one name="company" column="companyId" class="com.iknition.firstzk.beans.Company" outer-join="true" />
    </class>
</hibernate-mapping>

更新:

  • 我也参考了contact.hbm.xml,我错过了把它放在这里。
  • 我的意思是,当我试图构建应用程序时,我不断地得到"Caused by: org.hibernate.MappingException: entity class not found: com.iknition.firstzk.beans.Contact“的错误--我已经多次更改了这些配置文件的位置,但仍然出现了相同的错误。
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-25 07:48:39

我认为您必须逐个指定mappingLocations,例如:

代码语言:javascript
复制
<property name="mappingLocations">
  <util:list>
    <value>hibernate/Company.hbm.xml</value>
    <value>hibernate/Contact.hbm.xml</value>
  </util:list>
</property>
票数 1
EN

Stack Overflow用户

发布于 2012-05-24 18:34:42

嗯..。从未尝试使用外部hibernate.cfg.xml。但我认为指定只加载属性。您可能仍然需要在单独的属性中设置映射。

我的配置通常如下所示:

代码语言:javascript
复制
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <bean
            class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="propertiesArray">
                <list>
                    <props>...</props>
                </list>
            </property>
        </bean>
    </property>
    <property name="mappingLocations" value="classpath:com/iknition/firstzk/hibernate/*.hbm.xml"/>

</bean>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10741841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档