Search code examples
javasqldatabasejpajql

JPA: Differences between JQL and SQL


I need just an answer to a very specific question:

For example:

Customer c =(Customer) manager.createQuery("SELECT c FROM Customer c").getResultList().get(0);

This code works because of the use of the Java Persistence Query Language.

Customer c = (Customer) manager.createNativeQuery("SELECT * FROM Customer LIMIT 1").getSingleResult();

Using similar code and a native query casting to a Customer won't work, a ClassCastException will be raised.

That leads to the question: Is it possible to create objects as results from a native query using SQL?


Solution

  • I think you can, take a look at the documentation for createNativeQuery (the one with 2 params; Im having trouble with the link)

    just pass the class you want into the createNativeQuery call....