Lets say I have one table with two columns firstname
and lastname
with String datatype. Normally I write my hql query like
\"selec
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();}
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.
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"
You can also use || concatenation operator:
"select c.firstName || ' ' || c.lastName as fullName from Contact"
allthough it may be confusing to read.