Reputation: 7958
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.
Upvotes: 0
Views: 1401
Reputation: 22435
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.
Upvotes: 4