Execute a method before saving a model in Play Framework 1.x
-
12-12-2019 - |
Question
How do I execute a method just before saving a model?
Like for example hashing passwords in a User Model.
@Before
annotation does not seem to exist for models.
I am using PlayFramework 1.2.x
PS : I plan on using it with the CRUD Module.
Solution
Check out the collection of Pre*
annotations in JPA. For example PrePersist
and PreUpdate
.
In your model, just stick the annotation you need onto the method that you want to invoke.
@PrePersist
public void hashPassword(){
// do stuff
}
Another option might be to override the save()
method for the particular model, but I wouldn't do that unless it's completely necessary. I would stick to the JPA APIs if they offer a solution, but that is just my opinion.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow