-->
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.  [ 3 posts ] 
Author Message
 Post subject: Recursive categories with annotations (fetch size issue)
PostPosted: Sun Jan 08, 2006 9:07 am 
Beginner
Beginner

Joined: Mon Feb 07, 2005 10:40 pm
Posts: 22
Hi I have a recursive Relationship to Map Categories with annotations and after running the following query I get the full tree. I would like to just get the Children of a given Category and its Parent onlye when requested.
I have tried many combinations with fetch size, lazy intialziation etc, making the oneToMany the owning side etc... and nothing seems to work, I can make it work with hbm.xml mapping files but no with annotations.
Any help is appreciated.

Best Regards.

Raul Raja.


Here is the annotated class:
Code:

@Entity
@Indexed(index="/indexes/Category")
public class Category implements Serializable {
   private java.lang.Integer id;

   private java.lang.String title;

   private Category parent;

   private Set<Category> children = new HashSet<Category>();

   @Id(generate=GeneratorType.AUTO)
   @Keyword(id=true)
   public java.lang.Integer getId() {
      return id;
   }

   public void setId(java.lang.Integer id) {
      this.id = id;
   }


   @ManyToOne(fetch = FetchType.LAZY)
       @JoinColumn(name="parent_id", insertable=false, updatable=false)
   public Category getParent() {
      return parent;
   }

   public void setParent(Category parent) {
      this.parent = parent;
   }

   
   @OneToMany(fetch = FetchType.LAZY)
   @JoinColumn(name="parent_id")
   @OrderBy("id")
   public Set<Category> getChildren() {
      return children;
   }
   
   public void setChildren(Set<Category> children) {
      this.children = children;
   }


}




If I use this mapping file it works (but I have to do it with annotations):
Code:
<hibernate-mapping>

    <class name="com.estudiowebs.CMS.domain.Category" table="category">
        <id name="id" column="id" type="integer">
            <generator class="native"/>
        </id>

        <property name="title" column="title" type="java.lang.String" />

        <many-to-one name="parent" class="com.estudiowebs.CMS.domain.Category" cascade="none" outer-join="auto" column="parent_id" />

      
      <set name="children" lazy="true" inverse="false" cascade="save-update"
         order-by="id">

         <key column="parent_id" />

         <one-to-many
            class="com.estudiowebs.CMS.domain.Category" />
      </set>
       
    </class>
   
</hibernate-mapping>





Hibernate version:
3.1
Name and version of the database you are using:
MySQL 4.1


Code:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 12:30 am 
Beginner
Beginner

Joined: Mon Feb 07, 2005 10:40 pm
Posts: 22
Hi I think I have Isolated the problem. It pretty much looks like FetchType.LAZY is not working for @ManyToOne associations in my case:
I wrote a small test that loads a Child Category, event though I have:
Code:
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="parent_id")
    public Category getParent() {
      return parent;
    }


All the parent are loaded, here is the test:

Code:
public class TestLoadRecursiveCategories extends TestCase{
private EntityService getEntityService() {
  return (EntityService) RegistryBuilder.constructDefaultRegistry().getService(EntityService.class);
}

public void testLoadRecursiveCategories() {
      
  EntityService db = getEntityService();
  Category c = (Category) db.load(Category.class,203);
  System.out.println("Originally asked only for: " + c.getId());
}
   
}


And this is the output:

Code:
1 with id  1 com.estudiowebs.CMS.domain.Category loaded so far
2 with id  3 com.estudiowebs.CMS.domain.Category loaded so far
3 with id  28 com.estudiowebs.CMS.domain.Category loaded so far
4 with id  170 com.estudiowebs.CMS.domain.Category loaded so far
5 with id  203 com.estudiowebs.CMS.domain.Category loaded so far
Originally asked only for: 203


As you can see even though FetcType is set to Lazy, all parents are being loaded all the way until it reaches de root. My intention is that no parents are loaded until requested by calling getParent();

Below is the class that I'm using:

Code:

@Entity
public class Category implements Serializable {


   private java.lang.Integer id;

   private java.lang.String title;

   private Category parent;

   
   @ManyToOne(fetch = FetchType.LAZY)
       @JoinColumn(name="parent_id")
   public Category getParent() {
      return parent;
   }

   public void setParent(Category parent) {
      this.parent = parent;
   }

   @Id(generate=GeneratorType.AUTO)
   @Keyword(id=true)
   public java.lang.Integer getId() {
      return id;
   }

   public void setId(java.lang.Integer id) {
      this.id = id;
   }

}



I don't know if I am making a wrong assumption here, but if an association is set to Lazy should not be loaded until requested, right?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 2:52 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
check the HA unit test suite, and change the examples until you find your problem. This definitely works

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.