-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Mapping a composite pattern?
PostPosted: Fri Aug 18, 2006 9:05 am 
Newbie

Joined: Tue Sep 28, 2004 3:16 am
Posts: 1
Hi everyone,
I have encountered an issue of mapping my domain objects.
I have three classes:

Code:
public abstract class Item extends Entity {
    Item parent;
    String code;

    public Item getParent() {
        return parent;
    }
   
    public void setParent(Item parent) {
        this.parent = parent;
    }
   
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

}


public class Category extends Item {
    Set<Item> items;
   
    public Category() {
        items = new HashSet<Item>();
    }
   
    public Set<Item> getItems() {
        return items;
    }
   
    public void setItems(Set<Item> items) {
        this.items = items;
    }
   
    public void addItem(Item item) {
        if (item!=null) {
            items.add(item);
            item.setParent(this);
        }
    }
   
    public void removeItem(Item item) {
        if (item!=null) {
            items.remove(item);
            item.setParent(null);
        }
    }
}


public class Product extends Item {
    String unit;
    double salePrice; // VND
    double purchasePrice; // VND
   
    public String getUnit() {
        return unit;
    }
   
    public void setUnit(String unit) {
        this.unit = unit;
    }
   
    public double getPurchasePrice() {
        return purchasePrice;
    }
   
    public void setPurchasePrice(double importPrice) {
        this.purchasePrice = importPrice;
    }
   
    public double getSalePrice() {
        return salePrice;
    }
   
    public void setSalePrice(double sellPrice) {
        this.salePrice = sellPrice;
    }
}


...and I mapped these classes as follow:

Code:
<hibernate-mapping package="com.anant.ejournal.om">   
   <class name="Item" table="ITEM">
        <id
           name="id"
           column="ITEM_ID"
           type="long"
           unsaved-value="0">
           
            <generator class="native"/>
        </id>
       
        <property
           name="name"
           column="NAME"
           type="string"/>
       
        <property
           name="code"
           column="CODE"
           type="string"/>
           
        <many-to-one
           name="parent"
           class="Item"
           column="PARENT_ID"           
           cascade="none"/>       
    </class>
   
    <joined-subclass name="Category" table="CATEGORY" extends="Item">
       <key column="CATEGORY_ID"/>
       
       <set
          name="items"
          cascade="save-update"
          table="ITEM"
          inverse="true">
          <key column="PARENT_ID"/>
          <one-to-many class="Item"/>
       </set>
    </joined-subclass>
   
    <joined-subclass name="Product" table="PRODUCT" extends="Item">
       <key column="PRODUCT_ID"/>
       
       <property
          name="unit"
          column="UNIT"
          type="string"/>
       
       <property
          name="salePrice"
          column="SALE_PRICE"
          type="double"/>
       
       <property
          name="purchasePrice"
          column="PURCHASE_PRICE"
          type="double"/>
    </joined-subclass>
</hibernate-mapping>


...when I run a sample program as follow:

Code:
        ....
        Category category1 = new Category();
        Category category2 = new Category();
        Product product1 = new Product();
        Product product2 = new Product();
        Product product3 = new Product();

        category1.addItem(category2);
        category1.addItem(product1);
        category2.addItem(product2);
        category2.addItem(product3);
       
        dao.saveOrUpdate(category1);
        ....


...Exception was thrown. Here is the stack trace

Code:
Caused by: org.hibernate.MappingException: Unknown entity: com.anant.ejournal.om.Category
   at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)
   at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1302)
   at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
   at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:460)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:84)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:490)
   at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:672)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:361)
   at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:669)
   at com.anant.baseware.dao.hibernate.HibernateDAO.saveOrUpdate(HibernateDAO.java:76)
   at RCMScrapbook.main(RCMScrapbook.java:72)



I dont know if my mapping file is correct. Please help me. Thank you in advance!

I am using Hibernate version: 3.1.3


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 20, 2006 8:18 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Looks like you have a different mapping referring to Category? You can't do that, you need to refer to Item as it's the base class.

There's another bug in your code. You have inverse="true" on the Category->Item collection, but in your source code you're added transient Items to the collection. This won't work. Either remove inverse="true" (allowing Hibernate to persist the transient items), or save the items before adding them to the collection.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.