Java JPA本机查询插入返回id
我使用以下代码Java JPA本机查询插入返回id,java,jpa,persistence,Java,Jpa,Persistence,我使用以下代码 EntityManager em.createNativeQuery("insert into users (name,surname) values (\"Test\",\"Test\") returning id").executeUpdate(); 如果插入成功,executeUpdate方法返回我1,但是如何使用上面的查询获取新用户的id(我的db是Postgres) 您可以使用从Postgres获取返回的id,而不是executeUpdate 请参阅为什么要对这么简单
EntityManager em.createNativeQuery("insert into users (name,surname) values (\"Test\",\"Test\") returning id").executeUpdate();
如果插入成功,executeUpdate方法返回我1,但是如何使用上面的查询获取新用户的id(我的db是Postgres) 您可以使用从Postgres获取返回的
id
,而不是executeUpdate
请参阅为什么要对这么简单的东西使用本机查询,而不使用
em.persist(user)
?这是一个例子,我有一个大的postgres查询,无法转换为HQL或其他东西