-->
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: Mapped Expression
PostPosted: Fri Apr 01, 2005 7:07 pm 
Newbie

Joined: Fri Apr 01, 2005 6:47 pm
Posts: 11
I haven't been able to find an elegant way of handling this. I often display and filter a list of contact's based on their names. My database stores first_name and last_name as separate fields. However, I'd like to be able to represent the concatenation of these in my code as a single field called full_name.

I can easily add a getFullName() method to my hibernate class which returns the concatenation. However, then you can't filter and order by FullName (without manually creating special HQL). Is there any way to create something in the hibernate mapping file that maps an expression (like "first_name || ' ' || last_name") as a property of my hibernate class?

I tried this:
<property
column="FIRST_NAME || ' ' || LAST_NAME"
length="255"
name="FullName"
not-null="false"
type="string"
/>

This actually works as long as I don't have multiple instances of this hibernate class in my query. If I have multiple contacts in my query then I get an ambiguous column error, because the SQL generated looks like:

select contact1.FIRST_NAME || ' ' || LAST_NAME, contact2.FIRST_NAME || ' ' || LAST_NAME ...

This seems like it would be a common requirement for people so there must be a way to solve it elegantly.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 01, 2005 7:42 pm 
Newbie

Joined: Fri Apr 01, 2005 6:47 pm
Posts: 11
Nevermind. I was searching for the wrong word "expression". The word I should have searched for is "function". Found out this works exactly the way I want:

<property
name="FullName"
formula="LAST_NAME || ', ' || FIRST_NAME"
type="string"
update="false"
insert="false"
/>

It's a beautiful thing!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 01, 2005 7:43 pm 
Newbie

Joined: Fri Apr 01, 2005 6:47 pm
Posts: 11
Sorry, I meant I should have searched for "formula"...duh


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.