简体   繁体   中英

MappingException .hibernate mapping fails for classes in different packages

while using hibernate,I came across this problem mapping classes in two different packages.

I have a myapp.domain.ItemforSale class which is to be mapped to myapp.cart.CartItem

I created CartItem.hbm.xml as below

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

I have a unidirectional mapping from CartItem to ItemForSale entity.While running a test on saving ItemForSale instances to db..I got this error

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

I removed the package attribute from element and put fully classified class names.Still I get the same exception..

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

As a final attempt,I moved the CartItem to myapp.domain package and everything works!

Any idea why this happens?It won't make sense in my app to move myapp.cart.CartItem to myapp.domain..Any help greatly appreciated.

thanks

mark

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

This only means it is searching for CartItem on myapp.domain package but your class is actually in myapp.cart package. So basically the hibernate mapping that you have shown is correct, but I think there could be some issues on your java class. Could you please display your java classes related to your issue?

Or you haven't display other related hibernate mapping that could cause this exception.

Also make sure all mappings in hibernate.cfg.xml or Spring's applicationContext corresponds to actual mappings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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