Reputation: 3189
Lets say I have one table with two columns firstname
and lastname
with String datatype. Normally I write my hql query like
"select firstname,lastname from contact"
Can I write a hql query which concatenates both property ?
Maybe something like "select firstname+lastname as fullname from Contact"
Upvotes: 29
Views: 69176
Reputation: 299
You can also use || concatenation operator:
"select c.firstName || ' ' || c.lastName as fullName from Contact"
allthough it may be confusing to read.
Upvotes: 6
Reputation: 1732
You may create a calculated column in your entity:
@Formula(value = " concat(first_name, ' ', last_name) ")
private String fullName;
And in your HQL you just refer to this field as you would do to any other.
In your case, you can do:
"select fullName from Contact"
Upvotes: 26
Reputation: 81
I did it so with hql
public List<Contract> findContracts(String fullName) {
Query q = sessionFactory.getCurrentSession().createQuery("from Contract c where :fullName = concat(c.firstname, ' ', c.lastname)");
q.setString("fullName", fullName);
return q.list();}
Upvotes: 4
Reputation: 692231
select concat(c.firstname, c.lastname) as fullname from Contact c
or, if you want a separator:
select concat(c.firstname, ' ', c.lastname) as fullname from Contact c
See the documentation.
Upvotes: 56