playframework中的save()和_save()模型的方法 [英] save() and _save() model's methods in playframework
问题描述
在创建playramework的模型时,我们可以使用save()或_save()方法.为什么这两种方法在框架中都可用,这是什么原因? (在这种情况下,它们执行相同的操作-将对象保存到db).
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).
为什么这样问:我在其中进行某些验证时曾使用过save()方法,但是如果我的最终用户想保存而不进行验证,则可以使用_save().所以我问自己为什么有两种方法都是公开的.
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 he would want to save without validation. So I ask myself why there are two methods which are both public.
我已经这样处理了:问题在于找到放置位置 保存时进行验证.在 实际上我已经使用 @PrePersist注释的某些方法 我想确保在save()附近 验证代码将被调用 当坚持.所以现在我可以 save()和_save():)
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() :)
推荐答案
实际上,请看一下save()的代码:
Actually, look at the code of save():
/**
* store (ie insert) the entity.
*/
public <T extends JPABase> T save() {
_save();
return (T) this;
}
因此,它仅调用_save()并返回自身以链接调用.
_save是包含实际业务逻辑的函数.
save只是主动记录设计的一个更实用的外观.
为什么_save是公开的而不是受保护的?我真的不知道.
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()IMO,但是它返回void.就这些;)
_save() can be called without any problem IMO but it returns void. That's all ;)
这篇关于playframework中的save()和_save()模型的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!