I've got the maven build working (once you install the el-ri.jar in
your maven repo.) But I decided to post the key ingredients of the
problem here for those who don't want to get this running locally.

I switched for the in memory hypersonic db to mysql just so I could
visually inspect the data to make sure it conformed to my
expectations.  I was suprised to see that the data was being stored
correctly so maybe there is a problem with my hb query?

1.) Test data is created using the following:

       Category dogs = new Category("Dogs");
       Category terriers = new Category("Terriers");
       dogs.addChildCategory(terriers);
       categoryDao.store(dogs);

2.) Data is mapped as follows:

<hibernate-mapping package="org.apache.shale.petstore.domain">

   <class name="org.apache.shale.petstore.domain.Category">
     <id name="id"
         column="CATEGORY_ID">
       <generator class="native"/>
     </id>
     <property name="description"/>
     <set name="pets"
          table="PET"
          inverse="true"
          cascade="all-delete-orphan">
       <key column="CATEGORY_ID"/>
       <one-to-many class="Pet"/>
     </set>
     <set name="childCategories"
          table="CATEGORY"
          inverse="true"
          cascade="all-delete-orphan">
       <key column="PARENT_CATEGORY_ID"/>
       <one-to-many class="Category"/>
     </set>
     <many-to-one
         name="parentCategory"
         column="PARENT_CATEGORY_ID"
         class="Category"
         cascade="none"/>
   </class>

</hibernate-mapping>

3.) Category domain object looks like:

public class Category implements Serializable {

   private Set<Category> childCategories = new LinkedHashSet<Category>();
   private Set<Pet> pets = new HashSet<Pet>();
   private Category parentCategory;
   private String description;
   private Long id;

   public Category(String description) {
       this.description = description;
   }

   public Category() {}

   public void setDescription(String description) {
       this.description = description;
   }

   public String getDescription() {
       return description;
   }

   public Set<Category> getChildCategories() {
       return childCategories;
   }

   // Private accessor method provided for use in Hibernate
   private void setChildCategories(Set<Category> childCategories) {
       this.childCategories = childCategories;
   }

   public void addChildCategory(Category childCategory) {

       if (childCategory == null)
           throw new IllegalArgumentException("Null child category");

       if (childCategory.getParentCategory() != null)
           
childCategory.getParentCategory().getChildCategories().remove(childCategory);

       childCategory.setParentCategory(this);
       childCategories.add(childCategory);

   }

   public Set<Pet> getPets() {
       return pets;
   }

   // Private accessor method provided for use in Hibernate
   private void setPets(Set<Pet> pets) {
       this.pets = pets;
   }

   public void addPet(Pet pet) {

       if (pet == null)
           throw new IllegalArgumentException("Null pet");

       pet.changeCategory(this);
       pets.add(pet);
   }

   public Category getParentCategory() {
       return parentCategory;
   }

   public void setParentCategory(Category parentCategory) {
       this.parentCategory = parentCategory;
   }

   // Private accessor method provided for use in Hibernate
   private void setId(Long id) {
       this.id = id;
   }

   public Long getId() {
       return id;
   }

   public boolean equals(Object obj) {
       if (this == obj) {
         return true;
       }
       if (!(obj instanceof Category)) {
         return false;
       }
       Category rhs = (Category)obj;
       return new EqualsBuilder()
               .append(description, rhs.getDescription())
               //.append(parentCategory, rhs.getParentCategory())
               .isEquals();
   }

  public int hashCode() {
    return new HashCodeBuilder(17, 47).
      append(description).
      //append(parentCategory).
      toHashCode();
  }

4.) Code to retrieve the object looks like:

   public List<Category> getCategories() {
       return getHibernateTemplate().find("from Category category");
   }

Any ideas on this?  Since the data is being stored correctly I have
three theories:

A.) Hibernate query is incorrect.  The query above in #4 is so simple
this is hard to believe.

B.) The mapping is incorrect.  Again, since the data is being written
to the db correctly this doesn't seem right.

C.) The domain object is screwing things up after the hibernate query.
(The most likely option.)

Sean

Reply via email to