-->
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: Filter on entity association mapping vs FetchType.EAGER
PostPosted: Fri Oct 03, 2008 7:53 am 
Newbie

Joined: Fri Oct 03, 2008 2:30 am
Posts: 16
Location: Neuchâtel, Switzerland
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


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.