1

I want to write a JPA query from a native query. The Native query is as follows:

select alias.fname 
from (select name, fname, lname from student) alias 
where student_id='1';

I am unable to create a JPA query from this, any help is appreciated. Thanks.

Gas
  • 17,601
  • 4
  • 46
  • 93
Nelson
  • 13
  • 5
  • Can you replace the query with just `select fname from student where student_id='1'`? – ivan.sim Sep 17 '14 at 06:20
  • Just in case there is some particular point, what runtime environmet are you running? JDK? Web app server? etc...? – JCM Mar 17 '15 at 20:08

1 Answers1

0

Looking at the JPQL docs and Hibernate docs, subquery in the WHERE clause is not supported. You are left with the options of either:

  1. Simplifying your query to just select fname from student where student_id='1' (which I assume isn't your real query).
  2. Converting your query to use JOINs if possible, or
  3. Executing the native query using the JPA EntityManager.createNativeQuery() method. (Least preferred unless you really can't simplify your query.)
ivan.sim
  • 8,972
  • 8
  • 47
  • 63
  • No, actually this is not my actual query, my query is a very complex one. I am getting line exception here, so this is just the sample query and not the original one. – Nelson Sep 17 '14 at 12:40