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

标签 spring hibernate zk

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

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

我有这个结构

enter image description here

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

Company.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:

<?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:找不到实体类:com.iknition.firstzk.beans.Contact”我已经多次更改了这些配置文件的位置,但仍然收到相同的错误。

最佳答案

嗯...从未尝试过使用外部 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>

关于spring - applicationcontext.xml 和 .hbm 文件放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10741841/

相关文章:

spring - JavaEE 6 和 Spring

java - InternalViewResolver 在 Spring 中不工作

java - 本地安装 Spring 温室的问题

java - 使用 Flyway 在 Quarkus 上进行响应式 hibernate

java - 日期框自定义约束

java - 绑定(bind)请求映射到html中的提交输入按钮(Springboot)

java - 数据库字段验证客户端

java - JPA 如何识别两个具有相同名称但位于不同包中的类?

java - zk Controller 中的有线变量为空

java - 如何在执行同一击键的EventListener期间禁用ZK中的某个击键