问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/13581990/getting-generated-identifier-for-jpa-native-insert-query