Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6600051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:34:17+00:00 2026-05-25T18:34:17+00:00

How do I edit an existing object in the database? For example, if I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:34:18+00:00Added an answer on May 25, 2026 at 6:34 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to create an abstract representation of an existing object. For this example
I have the following code [in doPost()] to edit existing record. It does not
Edit: This question was written in 2008, which was like 3 internet ages ago.
I have a modelform that will either create a new model or edit an
I am trying to create a form to edit an existing database row. I
I have an existing database with lots of complex stored procedure and I want
We have an existing Firebird database which we wish to setup in an ASP.NET
Say you have something like an ASP.NET ASP:DetailsView to show and edit a single
I'm working on a system were a user can edit existing objects (Filter domain
I'm trying to determine how to open/edit existing SQL Server Reporting Services (SSRS) 2005

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.