-->
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.  [ 1 post ] 
Author Message
 Post subject: Nested Set
PostPosted: Wed Jan 19, 2011 4:55 am 
Newbie

Joined: Fri May 25, 2007 4:37 am
Posts: 13
Location: Germany
Hello!

Using Hibernate 3.5.6.

I have a Component entity with a map of Components, where a string field is used as the key:

Code:
@Entity
@Table(name = "myapp_component")
@Inheritance(strategy = InheritanceType.JOINED)
public class Component extends BaseEntity {

   @Column(name = "attribute_name", nullable = true, length = 128)
   private String attributeName;

   @ManyToOne(cascade = CascadeType.ALL, optional = true)
   @JoinColumn(name = "parent_id", nullable = true)
   private Component parent;

   @OneToMany(mappedBy = "parent")
   @MapKey(name = "attributeName")
   private Map<String, Component> children;

        // ... getters and setters ....
}


Another thing is, that I use inheritance. So there are more entities like ComponentText and ComponentImage which derive from Component. But I have no idea if that is important for my problem.

And the problem is: when I query for a Component which has some children, which themselves have children, Hibernate makes a lot of queries. It seems like for every Component one query is executed.

This is the first query:
Code:
Hibernate:
    select
        component0_.id as id1_1_,
        component0_.uid as uid1_1_,
        component0_.version as version1_1_,
        component0_.attribute_name as attribute4_1_1_,
        component0_.parent_id as parent5_1_1_,
        component0_.template_instance_id as template6_1_1_,
        component0_1_.image_id as image2_2_1_,
        component0_2_.text_value as text1_3_1_,
        case
            when component0_1_.component_id is not null then 1
            when component0_2_.component_id is not null then 2
            when component0_.id is not null then 0
        end as clazz_1_,
        children1_.parent_id as parent5_1_3_,
        children1_.id as id3_,
        children1_.attribute_name as formula0_3_,
        children1_.id as id1_0_,
        children1_.uid as uid1_0_,
        children1_.version as version1_0_,
        children1_.attribute_name as attribute4_1_0_,
        children1_.parent_id as parent5_1_0_,
        children1_.template_instance_id as template6_1_0_,
        children1_1_.image_id as image2_2_0_,
        children1_2_.text_value as text1_3_0_,
        case
            when children1_1_.component_id is not null then 1
            when children1_2_.component_id is not null then 2
            when children1_.id is not null then 0
        end as clazz_0_
    from
        myapp_component component0_
    left outer join
        myapp_component_image component0_1_
            on component0_.id=component0_1_.component_id
    left outer join
        myapp_component_text component0_2_
            on component0_.id=component0_2_.component_id
    left outer join
        myapp_component children1_
            on component0_.id=children1_.parent_id
    left outer join
        myapp_component_image children1_1_
            on children1_.id=children1_1_.component_id
    left outer join
        myapp_component_text children1_2_
            on children1_.id=children1_2_.component_id
    where
        component0_.id=?


And then for every child another query:
Code:
Hibernate:
    select
        children0_.parent_id as parent5_1_1_,
        children0_.id as id1_,
        children0_.attribute_name as formula0_1_,
        children0_.id as id1_0_,
        children0_.uid as uid1_0_,
        children0_.version as version1_0_,
        children0_.attribute_name as attribute4_1_0_,
        children0_.parent_id as parent5_1_0_,
        children0_.template_instance_id as template6_1_0_,
        children0_1_.image_id as image2_2_0_,
        children0_2_.text_value as text1_3_0_,
        case
            when children0_1_.component_id is not null then 1
            when children0_2_.component_id is not null then 2
            when children0_.id is not null then 0
        end as clazz_0_
    from
        myapp_component children0_
    left outer join
        myapp_component_image children0_1_
            on children0_.id=children0_1_.component_id
    left outer join
        myapp_component_text children0_2_
            on children0_.id=children0_2_.component_id
    where
        children0_.parent_id=?


Edit:
By the time I found out, that this is a common relational database problem with tables that have a parent-child relation with themselves.
A possible solution seems to be saving the Components in a Nested Set. Seems to be a tricky one though o_O
Does anyone have an example on how to do that?

Thank you!


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

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.