简体   繁体   English

使用 JpaRepository save() 方法的方法是否应该返回一些东西?

[英]Should method which use JpaRepository save() method return something?

I have a question about saving using JpaRepository.我有一个关于使用 JpaRepository 保存的问题。 I have a Repository for my entity, and its service realization.我的实体及其服务实现有一个存储库。 For instance:例如:

public Obj saveObj(Obj o) {
  return repo.save(obj);
}

Question: Is this a good approach to return Obj, if I don't set the saving result to other Obj?问题:如果我不将保存结果设置为其他 Obj,这是返回 Obj 的好方法吗? For instance:例如:

public void myMethod() {
  Obj obj = buildObj();
  servise.saveObj(obj);
}

The main cause of this return in saveObj(Obj o) method that I need it for testing.我需要它进行测试的saveObj(Obj o)方法中返回的主要原因。 Will be glad to your advices.很乐意听取您的建议。

I think the answer is 'it depends'.我认为答案是“视情况而定”。

A case where you'll want to return the object is when you're using either a sequence or auto incremental PK in your DB and you need the entities ID after it's been created.您想要返回 object 的情况是,当您在数据库中使用序列或自动增量 PK 并且在创建实体 ID 后需要实体 ID。

You may also want to return it if you need to pass it up your call stack to perform something like a ACL check or for logging.如果您需要将它传递到调用堆栈以执行 ACL 检查或日志记录等操作,您可能还需要返回它。

But there might be cases we're you just updating a single column (field) and you don't need keep the whole object around in memory.但在某些情况下,我们只是更新单个列(字段),而您不需要将整个 object 保留在 memory 中。

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

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