Image this little test case
Code:
@org.hibernate.annotations.FilterDefs(@org.hibernate.annotations.FilterDef(name = "childFilter"))
@Entity
public class Parent {
@Id @GeneratedValue private Long id;
@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
@org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SUBSELECT)
@org.hibernate.annotations.Filter(name = "childFilter", condition = "id = 2")
private Set<Child> childs = new HashSet<Child>();
public Set<Child> getChilds() { return childs; }
}
@Entity
public class Child {
@Id @GeneratedValue private Long id;
@ManyToOne()
@JoinColumn(name = "parentId")
private Parent parent;
}
The database looks like
Code:
Table Parent:
id
----
1
Table Child:
id parentId
---- ----------
1 1
2 1
3 1
After executing
Code:
session.enableFilter("childFilter");
Parent p = (Parent) session.get(Parent.class, 1L);
I expected that
Code:
p.getChilds().size() == 1
Unfortunately it's 3, because FetchType.EAGER wins and the filter is ignored. Ok the solution is to remove FetchType.EAGER and call
Code:
Hibernate.initialize(p.getChilds())
afterwards (the entity will be detached and sent to the client ...)
But that's a little annoying if the hierarchy is deeper than one level.
I didn't find a definition how this should behave in the hibernate documentation or in "Java Persistence with Hibernate".
So is this behavior a feature, a bug, hazard?
Tested on an JBossAS 4.2.2 GA using
hibernate 3.2.4.sp1
hibernate-entitymanager/-annotations 3.2.1.GA
Best regards,
Ron