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