Reputation: 39
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.
Upvotes: 0
Views: 812
Reputation: 12716
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.
Upvotes: 1