-->
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: Creating criteria for ManyToMany relationship
PostPosted: Tue Mar 16, 2010 1:02 pm 
Newbie

Joined: Tue Mar 16, 2010 12:37 pm
Posts: 1
I have a many-to-many relationship:
Code:
@Entity
@Table(name = "ITEM")
class Item {
    // Some properties...

    private String name;

    private Set<Category> categoryCollection = new HashSet<Category>(0);
   
    // Some getters and setters
   
    @ManyToMany
    @JoinTable(name = "ITEM_CATEGORY", joinColumns = {
            @JoinColumn(name = "ITEM_ID",
                    nullable = false, updatable = false) },
            inverseJoinColumns = {
            @JoinColumn(name = "CATEGORY_ID",
                    nullable = false, updatable = false) })
    public Set<Category> getCategoryCollection() {
        return categoryCollection;
    }
   
    public String getName() {
        return name;
    }

}   

@Entity
@Table(name = "CATEGORY")
class Category {
    // Some properties...

    private String name;

    private Set<Item> itemCollection = new HashSet<Item>(0);

    // Some getters and setters
   
    @ManyToMany(mappedBy = "categoryCollection")
    public Set<Item> getItemCollection() {
        return itemCollection;
    }
   
    public String getName() {
        return name;
    }

}

In my ItemDao class, i need to build a criteria: find items associated to a category (by name).
Code:
class ItemDao extends SpringHibernateDao<Item> {

    // ...
   
    public PagedList<Item> findByFilter(ItemFilter filter,
            Pagination pagination) {

        DetachedCriteria crit = DetachedCriteria.forClass(Item.class);
        Disjunction disjunction = Restrictions.disjunction();
       
        if (filter.getName() != null) {
            disjunction.add(Restrictions.eq("name", filter.getName())
                    .ignoreCase());
        }
       
        if (filger.getCategoryName() != null {
       
            // TODO: Need to add a disjunction restriction here.
           
        }
   
        // ...
    }
   
}

There are some alternatives that cannot be included in a disjunction restriction, like above:
Code:
DetachedCriteria dc = crit.createCriteria("categoryCollection");
dc.add(Restrictions.eq("name", filter.getCategoryName()).ignoreCase());

I found a solution but created a second DetachedCriteria for Item class:
Code:
DetachedCriteria dcItem = DetachedCriteria.forClass(Item.class, "i");
DetachedCriteria dcCategory = dcItem.createAlias("categoryCollection", "c");
dcCategory.add(Restrictions.eq("c.name", filter.getCategoryName()).ignoreCase());
dcCategory.setProjection(Projections.property("i.id"));
disjunction.add(Subqueries.propertyIn("id", dcCategory));

I think it's not a good solution. Anybody could show me a better solution?

Thanks


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.