Take the 2-minute tour ×

How do I edit an existing object in the database? For example, if I have a model like this: class Topic{title,content,author}, when I edit and save the object I do not want to add the "author" object again. How do I update my existing object rather than adding a new one?

share|improve this question
i think i didn't make myself clear, another example: my existing object is Topic(id:1,content:'xxx',author:'roy'...) ， now i pass an object like Topic(id:1,content:'yyy',author:null...)。how do i just update the not null fields ? does it have any easy way to do that ? –  MrROY Sep 27 '11 at 4:28
Oh, you only want to update certain fields and not others? Not easy, but see my answer below. –  andronikus Sep 27 '11 at 12:42
add comment

2 Answers

up vote 4 down vote accepted

If you're inheriting from the Model class (as you should), it provides a save() method as well as an ID attribute. When you call save() on an object you got out of the database, it will be updated, and if you call it on a new object it will be saved to the database. It's all automagical!

Updating only specific fields

Model.save() saves the whole object, so if you want to update only some data fields in your object, you first have to construct the exact object you want to go in your database. So let's say you don't want to update null fields, using your Topic(id, content, author) object:

Topic newT = Topic(1L, 'yyy', null);
Long id = newT.getID();

Topic oldT = Topic.findByID(id);  //Retrieve the old values from the database

Author newAuthor = newT.getAuthor(); //null

if (newAuthor != null) {  //This test will fail
  oldT.setAuthor(newAuthor);  //Update the old object

String newContent = newT.getContent(); //Not null

if (newContent != null) {
  oldT.setContent(newContent);  //Update the old object

// Now the object oldT holds all the new, non-null data.  Change the update tests as you see fit, of course, and then...

oldT.save();  //Update the object in the database.

That's how I would do it. Depending on how many fields you have this code is going to get really clunky really fast. Definitely put it in a method. Also, see here about findByID() and other ways to pull objects from the database.

share|improve this answer
In play 2.0.2, Model class provides update method also, does it work the same way as you described for save method to update particular column or I have to implement it the way you have described but that does not make a sense for update method. –  Neha Choudhary Nov 19 '12 at 8:05
add comment

If you look at the JPA Object Binding part of the documentation, you will see it explains that, if you pass in an object to your controller, containing an ID, the object is first automatically loaded from the database, and then the new fields passed in are added to the object.

This means, you can simply call the save() method.

So, you controller action could be something like

public static void editTopic(Topic topic) {
   //...any post edit processing
share|improve this answer
Great feature! Damn, I love the 1.2.4 version :-) –  adis Apr 10 '12 at 20:50
add comment

Your Answer


