-1

As I know, using SQL we can select the data with customised form like the query below:

select '<Root value=\"'"|| databaseColumnValue || '\"'" as displayItem from Table";

The selected value looks like below:

<Root value = 10 />

I have tried many ways in JPA to achieve this and haven't found one.

How can I achieve this kind of functionality using JPA ?

Makyen
  • 31,849
  • 12
  • 86
  • 121
ravinder reddy
  • 309
  • 1
  • 4
  • 17
  • It is not clear what is the actual issue here? What is your expected result? Is it a concatenation issue? Post your desired output. – Lalit Kumar B Mar 11 '15 at 13:59

1 Answers1

1

Try with CONCAT function (of course, adjust it to your entities)

select CONCAT('< Root value = ', myEntityField, ' />') as displayItem from MyEntity

I don't know what combinations did you try, but this works also, at least it does with Hibernate (thanks to @Lalit Kumar B for pointing this out)

 select '< Root value = ' || myEntityField || ' />' as displayItem from MyEntity
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • I am interested to know why the pipe operator used by OP won't work but `CONCAT` works? Concat function would do the same as the pipe opertor does, in fact concat function is limited to only two columns. – Lalit Kumar B Mar 11 '15 at 14:00
  • @LalitKumarB Actually, it should work with `||`, at least it does with Hibernate, but I'm not sure if it is supported by JPA (can't find anything in the spec). I'll edit my answer with another example. As for number of columns supported, two were supported in JPA `1.0`, from `2.0` you can put as much as you like. Taken from JPA 2.0 specification: `CONCAT(string_primary, string_primary {, string_primary}* )` – Predrag Maric Mar 11 '15 at 14:11