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: Exclude one field in query
PostPosted: Mon Feb 11, 2008 11:48 am 
Newbie

Joined: Mon Feb 11, 2008 11:40 am
Posts: 2
Hello all,

i want to exclude only on field from a query, how would i do that?

The problem with this is that sometimes i want the field to be included (if i specify a value for that field) and sometimes i don't want that field to be included (when there is no value specified for that field or it is empty)

The problem is when i use distinct keyword, that one field has to be excluded if it is not set.

I tried annotating the Entity's field with @Transient, which results in the correct behavior, but when i request a query with the transient parameter set the query crashes with:

Quote:
org.hibernate.QueryException: could not resolve property:


Is there a simple/clean solution for this or am i doing s.th. wrong?

If anybody could point me into the correct direction or has at least some keywords for me to search that would be nice ...

Regards,

Jens


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 11, 2008 8:58 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Add some custom code to the "getter" method of the property in question, (Hibernate goes through the getter method in order to retrieve the values) so that id doesn't bother your distincts, but you still can retrieve it every time.

for example:

Code:
  public String getName(){
    if (this.name==null || this.name.equals("")
      return "";
    else
      return this.name;
  }

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 6:17 am 
Newbie

Joined: Mon Feb 11, 2008 11:40 am
Posts: 2
Hi Gonzalo,

this doesn't seem to work at all, i implemented it the way you suggested,
but still the reults were the same, so i added a quick system.out to see if
the method is called at all, but that doesn't seem to be the case.

Maybe the problem is that i am using hibernate as a JPA provieder o s.th. like that? Or do i have to annotate the relevant fields with @Column, because
for some fields i ommited that (and it seems to work, except with the limitation of duplicates).

Do you have any other ideas where this problem might be located?

Regards,
Jens


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.