-->
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.  [ 3 posts ] 
Author Message
 Post subject: @Where annotation does not work for relations
PostPosted: Thu Aug 23, 2012 5:17 am 
Newbie

Joined: Thu Aug 23, 2012 4:28 am
Posts: 2
I have a Person table and a join table, containing people's relations. I have it set up to get people and their children, but I only want to get 'good' persons and filter out the 'evil' ones. To that end I have added a @Where annotation to my entity, like so:

The Person entity:
Code:
@Entity
@Table(name = "person")
@Where(clause = "personality <> 'EVIL'")
public class Person {
 
  @Id
  private int id;
 
  private String name;
 
  private String personality;
 
  @OneToMany
  @JoinTable(name = "relations",
      joinColumns = { @JoinColumn(name = "pid") },
      inverseJoinColumns = { @JoinColumn(name = "cid") }
  )
  private List<Person> children;

  // Getters & setters...

}


The test data:
Code:
--Evil parent, good child
INSERT INTO person (id, name, personality) VALUES (1, 'Darth Vader', 'EVIL');
INSERT INTO person (id, name, personality) VALUES (2, 'Luke Skywalker', 'GOOD');
INSERT INTO relations (pid, cid) VALUES (1, 2);

--Good parent, evil child
INSERT INTO person (id, name, personality) VALUES (3, 'Rosemary', 'GOOD');
INSERT INTO person (id, name, personality) VALUES (4, 'Rosemary\'s Baby', 'EVIL');
INSERT INTO relations (pid, cid) VALUES (3, 4);


Test code:
Code:
// Get evil parent - should be filtered out.
Person darthVader = personRepository.getPerson(1);
assertNull(darthVader);

// Get good parent - should be found.
Person rosemary = personRepository.getPerson(3);
assertNotNull(rosemary);

// Get children - should be filtered out.
List<Person> babies = rosemary.getChildren();
assertEquals(0, babies.size()); // <-- Assertion error! Finds Rosemay's baby!


In the log I can see that the extra where clause is present when fetching the parent...
Code:
select
    parent0_.id as id0_0_,
    parent0_.name as name0_0_,
    parent0_.personality as personality0_0_
from
    person person0_
where
    person0_.id=?
    and (
        person0_.personality <> 'EVIL'
    )


...whereas it is not, when fetching the children.
Code:
select
    children0_.pid as pid0_1_,
    children0_.cid as cid1_,
    person1_.id as id0_0_,
    person1_.name as name0_0_,
    person1_.personality as personality0_0_
from
    relations children0_
inner join
    person person1_
        on children0_.cid=person1_.id
where
    children0_.pid=?


How can I filter out the evil children as well?
Thanks!


Top
 Profile  
 
 Post subject: Re: @Where annotation does not work for relations
PostPosted: Thu Aug 23, 2012 7:19 am 
Newbie

Joined: Thu Aug 23, 2012 4:28 am
Posts: 2
Oh. I was supposed to put a @Where annotation on the relation as well:
Code:
@OneToMany
@JoinTable(name = "relations",
    joinColumns = { @JoinColumn(name = "pid") },
    inverseJoinColumns = { @JoinColumn(name = "cid") }
)
@Where(clause = "personality <> 'EVIL'")
private List<Person> children;

Solved!


Top
 Profile  
 
 Post subject: Re: @Where annotation does not work for relations
PostPosted: Fri Aug 24, 2012 3:16 am 
Newbie

Joined: Fri Aug 24, 2012 3:12 am
Posts: 2
Hi, Can't we add @Where only at field level rather than the Entity level

aznan wrote:
Oh. I was supposed to put a @Where annotation on the relation as well:
Code:
@OneToMany
@JoinTable(name = "relations",
    joinColumns = { @JoinColumn(name = "pid") },
    inverseJoinColumns = { @JoinColumn(name = "cid") }
)
@Where(clause = "personality <> 'EVIL'")
private List<Person> children;

Solved!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.