Getting generated identifier for JPA native insert query

末鹿安然 提交于 2019-12-24 03:39:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!