-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria CONCAT fields...
PostPosted: Wed Dec 21, 2005 2:30 pm 
Newbie

Joined: Thu Dec 15, 2005 10:18 am
Posts: 3
Location: São Paulo, Brasil
How i can concanete some fields with criteria API ?

sql example:
select * from person where concat(person.firstName,' ',person.middleName,' ',person.lastName) like '%aPerson%'


i try something like this... but doesn't work...

Criteria criteria = session.createCriteria(Person.class);
criteria.add(Expression.ilike("firstName middleName lastName ", keyWord));
criteria.list();

org.hibernate.QueryException: could not resolve property: firstName middleName lastName of: br.usp.lsi.contact.Person

tnx...

Markus Reichel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 6:55 am 
Newbie

Joined: Mon Feb 19, 2007 5:10 am
Posts: 15
Is there any resolution on this? I've just hit the same problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 8:28 am 
Newbie

Joined: Mon Feb 19, 2007 5:10 am
Posts: 15
I have found a workaround. I define a property formula in the mapping file which does the concatenation:

Code:
    <property name="ForenameSurname" formula="forename + ' ' + surname" type="String"/>
    <property name="PreferredNameSurname" formula="forename + ' ' + surname" type="String"/>


I can then refer to these properties in my search code:

Code:
ICriterion matchName =
  Expression.Or(
    Expression.InsensitiveLike("contact.ForenameSurname", filter.Member, MatchMode.Anywhere),
    Expression.InsensitiveLike("contact.PreferredNameSurname", filter.Member, MatchMode.Anywhere)
  );


Top
 Profile  
 
 Post subject: Re: Criteria CONCAT fields...
PostPosted: Fri Feb 24, 2012 1:03 pm 
Newbie

Joined: Fri Feb 24, 2012 12:32 pm
Posts: 1
I know this thread is old, but it is the one Google brought me to. Here is a solution using annotations. Note the formula is specific to Oracle (Using the double pipe "||" to concat).

From Person class:

Code:
@Column(name="first_name")
protected String firstName;

@Column(name="last_name")
protected String lastName;

@Formula(value="first_name || ' ' || last_name")
protected String fullName;


From the query code:

Code:
Criteria criteria = session.createCriteria(Person.class);
criteria.add(Restrictions.ilike("fullName", keyWord));
criteria.list();


Top
 Profile  
 
 Post subject: Re: Criteria CONCAT fields...
PostPosted: Sun Feb 03, 2013 2:20 pm 
Newbie

Joined: Wed Feb 21, 2007 1:48 pm
Posts: 8
It is possible to do this without formulas.

I posted an entry on my blog explaining how to do this:
http://whaticode.com/2013/02/01/hibernate-criteria-concatenate-fields-in-like-and-ilike-operators/
(too big to post here I believe).

It consists of creating a new Criterion for hibernate.
Appreciate any other ideas on the matter.


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