开发者

save() and _save() model's methods in playframework

When create playramework's model we can use save() or _save() method. Why these both methods are avalible in the framework, what's the reason? (in this context they do the same - save object to db).

Why I ask this: I have used save() method when doing some validation in it, but the end-user of my class could use _save() if h开发者_StackOverflow中文版e would want to save without validation. So I ask myself why there are two methods which are both public.

I've handled it like this: The problem was with finding the place for making the validation while saving. In fact I've handled this issue using @PrePersist anotation for some method near the save() when I want to be sure that validation code would be invoced when persisting. So now I'm ok with save() and _save() :)


Actually, look at the code of save():

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

So it simply calls _save() and return itself in order to chain calls.
_save is the function containing the real business logic.
save is just a more practical facade for active record design.
Why is _save public and not protected for example ? I don't really know.

_save() can be called without any problem IMO but it returns void. That's all ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜