2

For performance reasons I need to use some native queries to insert new objects into a database. I have a very sophisticated JPA persistence layer which manages my usual entity reading and writing and I would like to use this persistence layer to run the native queries.

I know I need the methods EntityManager#createNativeQuery(String) and Query#executeUpdate to achieve my goal.

My current problem is that I would also like to get the generated identifier of the newly inserted row. I know this is possible using JDBC or SpringPersistence but is there any way to get those identifiers from a JPA native query?

ali
  • 1,475
  • 4
  • 22
  • 40

1 Answers1

2

Most often this is DB-specific and if you can do it using JDBC you can do it also with JPA native query. You basically need to execute (the DB-specific) extra query to get the inserted ID.

Adam Dyga
  • 8,666
  • 4
  • 27
  • 35
  • forexample i have insert query and select last inserted id in method. will they execute those quries in the same transaction? please tell me. – Kumaresan Perumal Mar 04 '16 at 06:51