-->
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: search by parent entity
PostPosted: Tue May 12, 2009 6:14 am 
Newbie

Joined: Tue May 12, 2009 5:53 am
Posts: 1
Hi! i try to use hibernate search and have some troubles. Help me please.
I 've got two entities: File and Category. The File can belong to several categories. Categories have some hierarchy.

Code:
@Entity
@Indexed
@Table(name = "file")
public class File implements BusinessEntity {

........
@ManyToMany (cascade = {CascadeType.PERSIST})
    @JoinTable(
            name = "file_category",
            joinColumns = {@JoinColumn(name = "file_id", nullable = false)},
            inverseJoinColumns = {@JoinColumn(name = "category_id", nullable = false)}
    )
    @LazyCollection(LazyCollectionOption.FALSE)
    @Cascade(
            org.hibernate.annotations.CascadeType.PERSIST
    )
    @IndexedEmbedded
    public List<Category> getCategories() {
        return categories;
    }


Code:
@Entity
@Table(name = "cpa_category")
@Indexed
public class Category implements BusinessEntity {
...............
@OneToMany (mappedBy = "parent", cascade = CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    public List<Category> getChildrens() {
        return childrens;
    }


Now i can search by concrete categories id, but i want search by parent category id.
For example;
- Programming
- Java
- Swing
- AWT
And file in AWT category. So by AWT id i can find the file, but what if i need to search by Programming id and find all files of all children categories?
Thank you!


Top
 Profile  
 
 Post subject: Re: search by parent entity
PostPosted: Thu May 14, 2009 1:45 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
basically you have created a link from each file to a set of categories, containing the parents of all directly related categories. This link has to be fully traversed to identify all entities; you have the option to do this traversal at indexing time or at query time. IMHO is indexing time better for your application, but will make indexing slower.

The solution is to use @IndexedEmbedded on the getParent() in the Category, so that you will store the category ids and the parent ids in the index of File; set the prefix attribute to "" to make sure that the fields will have the same name (categories.id), indipendently of the depth in the tree.

Then you can filter on the selected category and it should be very efficient at query time. Using a filter with a category parameter will also enable caching.

_________________
Sanne
http://in.relation.to/


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.