简体   繁体   中英

Should method which use JpaRepository save() method return something?

I have a question about saving using 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? 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. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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