Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何在本机插入后获取新记录ID_Hibernate_Jpa_Spring Data Jpa - Fatal编程技术网

Hibernate 如何在本机插入后获取新记录ID

Hibernate 如何在本机插入后获取新记录ID,hibernate,jpa,spring-data-jpa,Hibernate,Jpa,Spring Data Jpa,出于一些好的原因,我进行了spring数据jpa本机插入。 查询以正确的方式执行。 但是我需要的是由nextval(“hibernate\u序列”)生成的新生成的表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 full

出于一些好的原因,我进行了spring数据jpa本机插入。
查询以正确的方式执行。
但是我需要的是由
nextval(“hibernate\u序列”)生成的新生成的表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的方式持久化数据

而是插入本机数据

无论如何,你们从insert语句返回数据的技巧非常酷。
我尝试了一下,它正在发挥作用。非常感谢
我最终得到了这个解决方案:

/**
 * 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);
有办法得到这个身份证吗

如果没有查询数据库,就不会有。除非您愿意使用普通JDBC

如果您的
文件附件
@ManyToOne工作机会
,为什么不执行以下操作:

FileAttachment attachment = new FileAttachment(file, fileName);
attachment.setJobOffer(entityManager.getReference(JobOffer.class, jobOfferId));
entityManager.persist(attachment);
entityManager.flush();
return attachment.getId();
这样,您将避免加载
JobOffer
的整个状态。如果关系是单向的,恐怕您必须检索整个
JobOffer

可选地,如果你真的必须使用一个原生的代码> INSERS/CODE >,考虑在你的数据库中定义一个存储过程,它将插入数据并返回自动生成的ID(参见)。p> 此外,一些数据库(例如PostgreSQL)允许从

INSERT
语句(
INSERT(..)到文件附件返回ID
)返回数据。您可能需要去掉
@Modifying
注释,因为插入的id最终会出现在查询的结果集中。您没有提到您使用的是哪个DB,但是语法看起来像Postgres,这就是我选择这个示例的原因。如果您正在使用另一个数据库,请查阅文档,可能有类似的功能


但是,我仍然建议不要在JPA应用程序中使用本机
INSERT
语句。很多东西都会断裂。我怀疑您试图实现的方法不是更大工作单元的一部分,但如果是这样,我会非常小心

只是好奇一下。。。entityManager.getReference(JobOffer.class,jobOfferId)在客户机/服务器环境中工作吗?这是什么意思?对不起,克里兹,我误解了getReference的某些内容。请不要再介意我的问题了。谢谢你的帮助!亲切的问候-托马斯。