-->
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.  [ 6 posts ] 
Author Message
 Post subject: Composite property?
PostPosted: Fri Aug 21, 2009 10:28 am 
Newbie

Joined: Tue Jul 07, 2009 12:33 pm
Posts: 5
I'm doing a search query (using criteria), using a search string like "Bruce Willis".

Code:
  <class name="Actor" table="ACTORS">
    <id column="ID_ACTOR" name="id" type="string">
      <generator class="identity"/>
    </id>   
    <property name="firstName" column="FIRSTNAME_ACTOR" />
    <property name="lastName" column="LASTNAME_ACTOR" />
  </class>


And in Java I WANT TO do this:
Code:
...
disjunction.add(Restrictions.ilike("artists.fullName", "Bruce Willis", MatchMode.START));
...

instead of
Code:
...
disjunction.add(Restrictions.ilike("artists.firstName", "Bruce", MatchMode.START));
disjunction.add(Restrictions.ilike("artists.lastName", "Willis", MatchMode.START));
...


How do I define "fullName" when I have two columns of firstName and lastName?


Top
 Profile  
 
 Post subject: Re: Composite property?
PostPosted: Fri Aug 21, 2009 12:46 pm 
Newbie

Joined: Tue Feb 24, 2009 8:39 pm
Posts: 19
it depends on your database SQL, by example in postgresql:

<property name="fullName" type="string"
formula="FIRSTNAME_ACTOR || ' ' || LASTNAME_ACTOR"/>

or in mysql:

<property name="fullName" type="string"
formula="CONCAT(FIRSTNAME_ACTOR , ' ' , LASTNAME_ACTOR)" />


Top
 Profile  
 
 Post subject: Re: Composite property?
PostPosted: Fri Aug 21, 2009 12:58 pm 
Newbie

Joined: Tue Jul 07, 2009 12:33 pm
Posts: 5
hi-tech wrote:
it depends on your database SQL, by example in postgresql:

<property name="fullName" type="string"
formula="FIRSTNAME_ACTOR || ' ' || LASTNAME_ACTOR"/>

or in mysql:

<property name="fullName" type="string"
formula="CONCAT(FIRSTNAME_ACTOR , ' ' , LASTNAME_ACTOR)" />


Using Oracle.


Top
 Profile  
 
 Post subject: Re: Composite property?
PostPosted: Fri Aug 21, 2009 2:07 pm 
Newbie

Joined: Tue Feb 24, 2009 8:39 pm
Posts: 19
I'll explain in depth:
You must use a formula inside a property, this formula must contain a database-dependent instruction to concatenate two strings.

So if you're using oracle you must try reading an oracle reference http://www.psoug.org/reference/string_func.html

This is (maybe) the whole enchilada:

Since Oracle Concat function can only have two parameters, you must try something like this:

<property name="fullName" type="string" formula="(CONCAT( CONCAT(FIRSTNAME_ACTOR , ' ') , LASTNAME_ACTOR))" />


Top
 Profile  
 
 Post subject: Re: Composite property?
PostPosted: Mon Aug 24, 2009 6:21 am 
Newbie

Joined: Tue Jul 07, 2009 12:33 pm
Posts: 5
Excellent. Thanks for your help, the problem has been solved!

I used the following:

Code:
<property name="fullName" type="string" formula="FIRSTNAME_ACTOR || ' ' || LASTNAME_ACTOR" />


This seems to work in Oracle.


Top
 Profile  
 
 Post subject: Re: Composite property?
PostPosted: Sun Feb 03, 2013 2:19 pm 
Newbie

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

I posted an entry on my blog explaining:
http://whaticode.com/2013/02/01/hibernate-criteria-concatenate-fields-in-like-and-ilike-operators/

I hope it helps. The lack of such functionality is really a turn-off.


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