User1230321
User1230321

Reputation: 1475

How to get 2 fields concatenated in hibernate?

Say, I've a table called "Contact" with "first_name" and "last_name" as columns in it. Basically, "select concat(c.firstname, ' ', c.lastname) as fullname from Contact c" is what I want to do in hibernate.

I can put the entire query inside createQuery and get desired output. But, I don't want to execute sql queries in hibernate. I found a similar post here "Can we concatenate two properties in Hibernate HQL query?". But this executes sql query and of course it has concat in where clause(I want in select clause).

Somebody please suggest an answer.

Upvotes: 2

Views: 4996

Answers (1)

StanislavL
StanislavL

Reputation: 57381

You can introduce an artificial field in your entity and mark it with @Formula annotation

e.g.

@Formula("concat(first_name,' ',first_name)")
private String fullname;

See an example here

Upvotes: 11

Related Questions