WoooHaaaa
WoooHaaaa

Reputation: 20460

How do I update a certain field of a Model in Playframework?

Suppose we have a User class, if I want to update it's name:

User user = User.findById(123); user.name = "someone"; user.save();

The generated SQL would be

update user as user0 set user0.name = ? user0.email = ? .....

That means Play didn't realize I just want to update a single field. Is there any way could make the generated SQL only update the specified fields ?

Upvotes: 0

Views: 796

Answers (3)

Seb Cesbron
Seb Cesbron

Reputation: 3833

You can use hibernate's dynamic update feature : http://www.mkyong.com/hibernate/hibernate-dynamic-update-attribute-example/

But remember that this have negative impact on performance as database cannot cache statements. See here for others explanations : Hibernate : dynamic-update dynamic-insert - Performance Effects

Upvotes: 0

biesior
biesior

Reputation: 55798

Info: this is answer for Play 2 + Ebean! so it DOES NOT work with Play 1 + JPA

There are some options in Ebean's API, so you should check it and choose one:

Upvotes: 1

Kayaman
Kayaman

Reputation: 73568

This is dependent on whether Play (or actually Avaje Ebean) actually tracks which of the fields are changed. It's simpler to update all of the fields instead.

This isn't an issue performance-wise either, so I wouldn't waste too much time looking for a solution.

Upvotes: 0

Related Questions