Save () and _save () in playframework

When creating a playramework model, we can use the save () or _save () method. Why are both of these methods available within the framework, what is the reason? (in this context, they do the same thing - they save the object in db).

Why I ask about this: I used the save () method when doing some validation in it, but the end user of my class could use _save () if it wanted to save without validation. Therefore, I ask myself why there are two methods that are publicly available.

I dealt with it this way: the problem was finding a place for doing validation while saving. In fact, I dealt with this problem using @PrePersist Anonymity for some method next to save (), when I want to make sure that the verification code will be called when saving. So now I'm fine with save () and _save () :)

+4
source share
1 answer

Actually, look at the save () code:

/** * store (ie insert) the entity. */ public <T extends JPABase> T save() { _save(); return (T) this; } 

Therefore, it simply calls _save () and returns itself to the chain of calls.
_save is a function that contains real business logic.
save is just a more practical facade for active record design.
Why is _save public and not protected, for example? I really do not know.

_save () can be called without any IMO problems, but it returns void. It's all;)

+5
source

Source: https://habr.com/ru/post/1345061/


All Articles