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!
|