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 8148215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:36:12+00:00 2026-06-06T14:36:12+00:00

I´ve got a controller which gets an JsonObject from a POST method, parses it

  • 0

I´ve got a controller which gets an JsonObject from a POST method, parses it to a Model object and saves it.

The main problem is that I have to use the database object, apply the changes over it with the new object values and then save the old one. This wouldn’t be a problem if they where 2 properties but User is a big object and I want to make some clean code there.

This is the code:

public static void userUpdate(String apikey, JsonObject body) {
    Long idUser = decode(apikey);
    User oldUser= User.findById(idUser);

    Map<String, User> userMap = new HashMap<String, User>();
    Type arrayListType = new TypeToken<Map<String, User>>(){}.getType();
    userMap = gson().fromJson(body, arrayListType);
    User user = userMap.get("user");

    oldUser.cif = user.cif;
    oldUser.date_last_mod = user.date_last_mod;
    oldUser.save();
}

Is there a way to make a merge(oldUser, user) and make oldUser.save()? Or any other ideas?

Thanks all for the help! 🙂

  • 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-06-06T14:36:13+00:00Added an answer on June 6, 2026 at 2:36 pm

    I solved it like this.

    public static void userUpdate(String apikey, JsonObject body) {
        Long idUser = decode(apikey);
        User oldUser= User.findById(idUser);
    
        Map<String, User> userMap = new HashMap<String, User>();
        Type arrayListType = new TypeToken<Map<String, User>>(){}.getType();
        userMap = gson().fromJson(body, arrayListType);
        User user = userMap.get("user");
    
    oldUser.em().merge(user);
    
    oldUser.save();
    }
    

    You can get the EntityManager to make use of the merge option, I´ve to make some test but the first conclusion I make is that the object you are calling the merge for is the one who´s the slave and the one you pass as parameter is the master object which will update oldUser with the new values.

    Thanks anyway 🙂

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

Sidebar

Related Questions

I've got a controller class which accpets multiple parameters in the ctor which gets
i got an NSArray which gets filled in the init Method of my UITableViewController.
I've got a model Employee which belongsTo an Address-model. When I fetch data from
In one of my CI application I got a controller called movies in which
I've got a navigation controller based app that downloads an RSS feed and lays
I've got a lot controller in my Codeigniter apps, ex: Signup, Profile, Main, etc..
I have an app that I just got setup with a split view controller
From my jQuery file, I call a function from my controller which returns a
I've got a Controller ActionResult that looks like this Function Edit(ByVal user As Domain.User,
I've got a Grails controller which relies on the message taglib to resolve an

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.