Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 he 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() :)

like image 502
ses Avatar asked Mar 24 '11 10:03

ses


People also ask

What is activator in play framework?

The activator command can be used to create a new Play application. Activator allows you to select a template that your new application should be based off. For vanilla Play projects, the names of these templates are play-scala for Scala based Play applications, and play-java for Java based Play applications.

What is MVC in play framework?

A play application follows the MVC architectural pattern applied to the Web architecture. This pattern splits the application into separate layers: the Presentation layer and the Model layer. The Presentation layer is further split into a View and a Controller layer.

Why play framework is used?

Play Framework makes it easy to build web applications with Java & Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.

Is Play framework asynchronous?

Internally, Play Framework is asynchronous from the bottom up. Play handles every request in an asynchronous, non-blocking way. The default configuration is tuned for asynchronous controllers.


1 Answers

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 ;)

like image 168
mandubian Avatar answered Sep 29 '22 06:09

mandubian