3

我正在学习 spring hibernate zk 堆栈,并按照本教程做我的第一个 crud找到我的pojos。

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

我有这个结构

在此处输入图像描述

应用程序上下文.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>

休眠.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”我已经多次更改了这些配置文件的位置,但仍然出现相同的错误。
4

2 回答 2

2

嗯...从未尝试过使用外部 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>
于 2012-05-24T18:34:42.490 回答
1

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

<property name="mappingLocations">
  <util:list>
    <value>hibernate/Company.hbm.xml</value>
    <value>hibernate/Contact.hbm.xml</value>
  </util:list>
</property>
于 2012-05-25T07:48:39.790 回答