【发布时间】:2018-10-08 01:14:49
【问题描述】:
出于一些很好的原因,我做了一个 spring data jpa 本机插入。
查询以正确的方式执行。
但我需要的是由nextval('hibernate_sequence') 生成的新生成的表ID
有没有办法得到这个id?
这是我的查询:
/**
* Inserts a new file attachment.
* This is useful and maybe better suitable, because one may not want to load the whole
* job offer to fullfill the JobOffer -> FileAttachment OneToMany Relation
*
* @param file a given file
* @param fileName a given file name
* @param jobOfferId a given job offer id
* @return int the new id
*/
@Modifying
@Query(value = "insert into fileattachment(fileattachmentid, file, filename, joboffer_jobofferid) values (nextval('hibernate_sequence'), ?1, ?2, ?3);", nativeQuery = true)
int insertFileAttachment(String file, String fileName, long jobOfferId);
int 返回值只是给出插入记录的数量(1)。
但我需要新的身份证。
我不想在插入另一个数据库查询后查询它。因为如果我必须这样做,整个原生插入就会过时。
有人知道答案/有其他提示吗?
谢谢!
亲切的问候
托马斯
编辑:
我使用原生插入避免加载整个joboffer记录,这是很多无用的数据,只是为了将数据以entitymanager的方式持久化。
相反,我插入本地数据。
无论如何,从插入语句返回数据的提示非常酷。
我正在尝试,它正在工作。非常感谢!
我最终得到了这个解决方案:
/**
* Inserts a new file attachment.
* This is useful and maybe better suitable, because one may not want to load the whole
* job offer to fullfill the JobOffer -> FileAttachment OneToMany Relation
*
* @param file a given file
* @param fileName a given file name
* @param jobOfferId a given job offer id
* @return int the new id
*/
@Query(value = "insert into fileattachment(fileattachmentid, file, filename, joboffer_jobofferid) values (nextval('hibernate_sequence'), ?1, ?2, ?3) returning fileattachmentid;", nativeQuery = true)
long insertFileAttachment(String file, String fileName, long jobOfferId);
【问题讨论】:
标签: hibernate jpa spring-data-jpa