-->
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: Filtering a collection on an associated class.
PostPosted: Fri Sep 04, 2009 11:47 am 
Newbie

Joined: Fri Sep 04, 2009 11:22 am
Posts: 2
Hi,

I've been searching around and trying for a little while to sort this out. I'm trying to filter a collection of objects on a field that is on a class (table) referenced from the objects in the list.

I have:
Code:
@Entity
@Table(name="metadata")
public class MetaData implements Cloneable {
    ...

    private int permission;

    ...

}

Code:
@Entity
@Table(name="menuItems")
public class MenuItem {

    @OneToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="metaDataId")
    private MetaData metaData;

    ...
}

Code:
@Entity
@Table(name="menus")
@FilterDef(name="securityFilter", parameters=@org.hibernate.annotations.ParamDef( name="permission", type="integer" ) )
public class Menu {

    private String name;
    private int role;

    @OneToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="metaDataId")
    private MetaData metaData;
   
    @OneToMany(mappedBy="menu", fetch=FetchType.EAGER)
    @Filter(name="securityFilter", condition=":permission >= metadata.permission")   
    private List<MenuItem> menuItems;

    ...


I want the filter to reference the permission field on the metaData object held in the MenuItem class but can not figure out how to do that. I can use the Criteria API with a join to filter based on the MetaData for the Menu class but can't figure out how to go a level lower and filter the collection. I could probably hard code multiple joins in the Criteria API but I'm using a generic DAO and am trying to make it class specific.

Does anybody have any ideas on how to make a filter work in this way or is it impossible.

The way the SQL is generated Hibernate prepends a generated table name, I'm guesing this is to stop duplicate field names, however it also means you don't know what the name of the field will be:
Code:
select this_.id as id7_1_,
       this_.metaDataId as metaDataId7_1_,
       this_.name as name7_1_,
       this_.role as role7_1_,
       ssobjectme2_.id as id8_0_,
       ssobjectme2_.groupId as groupId8_0_,
       ssobjectme2_.groupPermission as groupPer3_8_0_,
       ssobjectme2_.lastModDate as lastModD4_8_0_,
       ssobjectme2_.lastModUser as lastModU5_8_0_,
       ssobjectme2_.memberId as memberId8_0_,
       ssobjectme2_.ownerPermission as ownerPer7_8_0_,
       ssobjectme2_.worldPermission as worldPer8_8_0_
from menus this_
left outer join metadata ssobjectme2_ on this_.metaDataId=ssobjectme2_.id
where this_.name=?


Do I need to move the columns from the metadata table to every table in the database to access the fields directly? This isn't really something I want to do on a large database, or to generate a view for each table...

Thanks in advance for your advice.
Peter


Top
 Profile  
 
 Post subject: Re: Filtering a collection on an associated class.
PostPosted: Sun Sep 06, 2009 10:05 am 
Newbie

Joined: Fri Sep 04, 2009 11:22 am
Posts: 2
Does anybody have any ideas? Do I just have to move those fields out of the related table onto each of the other tables?

Peter


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.