简体   繁体   English

如何获取JPA保存的object的id?

[英]How to get the id of saved object by JPA?

I want to get the ID of the object saved to the database by JPA.我想获取JPA保存到数据库的object的ID。 Consider an object person , I save this object using考虑一个 object person ,我保存这个 object 使用

personrepo.save(person)

How can I get the ID of this object?我怎样才能得到这个 object 的 ID?

Note: ID is the only unique column in this object.注意:ID 是此 object 中唯一的唯一列。

If you work with Spring Data, method save returns persisted object with generated ID.如果您使用 Spring 数据,方法 save 返回持久的 object 和生成的 ID。 So:所以:

person = personrepo.save(person);
var id = person.getId();

For more details, see documentation .有关更多详细信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM