获取JPA本机插入查询的生成的标识符 [英] Getting generated identifier for JPA native insert query
问题描述
出于性能原因,我需要使用一些本机查询将新对象插入数据库.我有一个非常复杂的JPA持久层,可以管理我通常的实体读写,并且我想使用该持久层来运行本机查询.
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.
我知道我需要EntityManager#createNativeQuery(String)和Query#executeUpdate方法来实现我的目标.
I know I need the methods EntityManager#createNativeQuery(String) and Query#executeUpdate to achieve my goal.
我当前的问题是我还想获取新插入的行的生成的标识符.我知道可以使用JDBC或SpringPersistence来实现,但是有什么方法可以从JPA本机查询中获取那些标识符?
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?
推荐答案
大多数情况下,这是特定于DB的,如果可以使用JDBC进行操作,也可以使用JPA本机查询来进行操作.基本上,您需要执行(特定于数据库的)额外查询以获取插入的ID.
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.
这篇关于获取JPA本机插入查询的生成的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!