【问题标题】:where to put applicationcontext.xml and .hbm files?在哪里放置 applicationcontext.xml 和 .hbm 文件?
【发布时间】:2012-05-24 16:46:51
【问题描述】:

我正在学习 spring hibernate zk stack,并在 this tutorial 之后做我的第一个 crud 我将 applicationContext.xml 放入 webapp/WEB-INF,将 .hbm.xml 放入 resources/mappings 但我不知道为什么我的 hbm 文件一直显示找不到我的 pojos。

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

我有这个结构

applicationContext.xml

  <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

    <mapping resource="com/iknition/firstzk/hibernate/Company.hbm.xml" />
    <mapping resource="com/iknition/firstzk/hibernate/Contact.hbm.xml" /> 

公司.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 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>

联系人.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 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,没放在这里。
  • “为什么我的 hbm 文件一直显示找不到我的 pojos”我的意思是,当我尝试构建应用程序时,我不断收到“Caused by: org.hibernate.MappingException: entity class not found: com.iknition.firstzk.beans.Contact”的错误,我已经更改了很多次这些配置文件的位置,仍然得到同样的错误。

【问题讨论】:

  • 你可以扩展这个:“为什么我的 hbm 文件一直显示找不到我的 pojos”?您遇到什么问题?此外,您仅在 hibernate.cfg.xml 中引用 Company.hbm.xml。 Contact.hbm.xml 呢?
  • 抱歉,我已更新问题以澄清您的问题。

标签: spring hibernate zk


【解决方案1】:

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

这是我的配置通常的样子:

<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>

【讨论】:

  • 我一直在玩位置和配置 applicationContext 文件,但我仍然遇到同样的错误。我已经更新了这个问题。我怀疑问题出在我的 .hbm 配置中
【解决方案2】:

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

<property name="mappingLocations">
  <util:list>
    <value>hibernate/Company.hbm.xml</value>
    <value>hibernate/Contact.hbm.xml</value>
  </util:list>
</property>

【讨论】:

  • 尝试了注释并且有效,然后我更改了您的建议并且也有效! :D
  • 好的。查看 LocalSessionFactoryBean 的文档。它需要一个 Resource[] 数组。而且你不能用 *.hbm.xml 链接它们。
  • 注释不正确,因为您在 pom.xml 中为 Java 1.4 配置了项目
  • 你说得对,我把它改成了jdk 1.6。但是对于每个配置路径,我必须在前面加上“类路径:”这个词才能使它们工作,即使它们都在资源文件夹中。 ej。我必须输入'classpath:hibernate/Contact.hbm.xml',否则它不会编译,因为找不到文件异常
  • 如果你使用'mappingResources'属性,你是正确的,但如果我相信'mappingLocations'允许使用通配符。
猜你喜欢
  • 1970-01-01
  • 2011-05-04
  • 2019-07-01
  • 1970-01-01
  • 2015-04-09
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
相关资源
最近更新 更多