繁体   English   中英

MappingException.hibernate 映射在不同包中的类失败

[英]MappingException .hibernate mapping fails for classes in different packages

在使用 hibernate 时,我在两个不同的包中遇到了这个问题映射类。

我有一个 myapp.domain.ItemforSale class 将被映射到 myapp.cart.CartItem

我创建了 CartItem.hbm.xml 如下

<hibernate-mapping package="myapp.cart">
<class name="CartItem" table="CARTITEM">
    <id name="cartItem_id" column="CARTITEM_ID" type="long">
        <generator class="native"/>
    </id>
    <property name="quantity" type="int" column="QUANTITY" />

    <many-to-one name="itemforsale" class="myapp.domain.ItemforSale" column="ITEM_FORSALE_ID" lazy="false" />
</class> 
...

我有一个从 CartItem 到 ItemForSale 实体的单向映射。在运行将 ItemForSale 实例保存到 db 的测试时。我收到此错误

506 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
506 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
571 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : myapp/domain/ItemForSale.hbm.xml
636 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: bookshop.domain.ItemForSale ->ITEMFORSALE
...
746 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : myapp/cart/CartItem.hbm.xml
751 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: myapp.cart.CartItem -> CARTITEM
...
org.hibernate.MappingException: Association references unmapped class: myapp.domain.CartItem

我从元素中删除了 package 属性,并放置了完全分类的 class 名称。我仍然得到相同的异常..

<hibernate-mapping>
<class name="myapp.cart.CartItem" table="CARTITEM">
    <id name="cartItem_id" column="CARTITEM_ID" type="long">
        <generator class="native"/>
    </id>
    <property name="quantity" type="int" column="QUANTITY" />

    <many-to-one name="itemforsale" class="myapp.domain.ItemForSale" column="ITEM_FORSALE_ID" lazy="false" />
</class>

作为最后的尝试,我将 CartItem 移动到 myapp.domain package 并且一切正常!

知道为什么会发生这种情况吗?在我的应用程序中将 myapp.cart.CartItem 移动到 myapp.domain 是没有意义的。非常感谢任何帮助。

谢谢

标记

org.hibernate.MappingException: Association references unmapped class: myapp.domain.CartItem

这仅意味着它正在 myapp.domain package 上搜索 CartItem,但您的 class 实际上在 myapp.cart package 中。 所以基本上你显示的 hibernate 映射是正确的,但我认为你的 java class 可能存在一些问题。 您能否显示与您的问题相关的 java 类?

或者您没有显示可能导致此异常的其他相关 hibernate 映射。

还要确保 hibernate.cfg.xml 或 Spring 的 applicationContext 中的所有映射对应于实际映射。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM