【发布时间】:2020-05-13 20:31:43
【问题描述】:
我有一个Document 实体。要编辑文档,您必须获得DocumentLock。
编辑文档的请求 API 如下所示。 edit=true 允许使用 'FOR UPDATE` 从数据库中获取记录。
GET /api/document/123?edit=true
在后端,我们这样做(当然过于简化),
Document document = documentRepository.getOne(documentId) //<--- (1)
if(document == null){ //<--- (2) This is always false
//Throw exception
}
DocumentLock dl = DocumentLock.builder()
.lockedBy(user)
.lockedAt(NOW)
.document(document)
.build()
documentLockRepository.save(dl);
我们使用getOne(),因为我们不需要从数据库中获取整个文档,我们只是创建与DocumentLock 对象的关系。
问题是,如何保证文件确实存在?因为getOne() 将始终返回代理,我没有看到任何明显的方法来检查数据库中是否存在记录。
版本:
- spring-data-jpa: 2.2.6.RELEASE
- 休眠:5.4.12
更新:删除了其他问题。
【问题讨论】:
标签: spring hibernate spring-boot jpa spring-data-jpa