In the api doc for play.db.jpa.Model ,the description for the method refresh() is given as ‘Refresh the entity state.‘..
In the documentation for jpa,the following snippet is given
public static void save(Long id) {
User user = User.findById(id);
user.edit("user", params.all());
validation.valid(user);
if(validation.hasErrors()) {
// Here we have to explicitly discard the user modifications...
user.refresh();
edit(id);
}
show(id);
}
In the paragraph above,it is given
we must tell the EntityManager which Objects NOT to update. We do this
by calling refresh(), which essentially rolls back a single entity. We
do this just prior to calling commit on the transaction or when we
realise the Object shouldn’t be updated.
Reading the api doc for the method ,I came to understand that by calling refresh(), the model state will be set to what was in the database.
But reading those snippet/paragraph ,I am getting confused..
Can someone please tell me what refresh() does?
I coded an app like,
...
Order lastOdr = Order.find("some query to find the last pending order..").first();
if(lastOdr!=null) {
lastOdr.refresh();
}
...
Is this wrong?I am trying to get the Order object from the database by executing that query..
In that code snippet:
will populate the entity with http request parameters which name start with “user”, say if the request has a parameter called “user.firstName”, then that value will be used to populate the user model’s firstName field.
is called when
validation.hasErrors(), meaning it will use the current database values to refill the user model and discard all changes coming formuser.edit("user", params.all());. However I don’t think this statement is needed because Play will not commit the changes unless you explicitly calluser.save().There is no need to call
refresh()in your code, as you have just fetch it from database, the state is supposed to be the same as it is in the database