HI,
I have query like this
final Query contractQuery = cgnDao.getEntityManager().
createNativeQuery("SELECT k.phrase, ak.type FROM key k INNER JOIN adkey ak USING (key_id) WHERE pck.pub_id =" + pid +" AND pck.c_id =" + campId );
How can i get each and every element from the query?
Where phrase is an String and type is enum
in Java
Thanks
解决方案
In short, you have a Query on which you can call getResultList(), which returns a List you can iterate over. Have a look at the JPA javadoc.
Also, I'd recommend using PreparedStatements, something like
String sqlQuery = "select * from tbl_spaceship where owner = ?";
Query q = entityManager.createNativeQuery(sqlQuery, SpaceShip.class);
q.setParameter( 1, "Han" );
q.getResultList();
The advantage is that the JPA provider will take care of escaping the input values. Not necessary at this particular use-case but good habits never hurt.