This example shows how changes are merged from the disconnected object to the object to update. To do this, traverse the object-graph and copy all value types over. All reference types are first checked if they are an existing object. If it is,the primitives are copied over, otherwise it's a stored as a new object.
ObjectContainer container = openDatabase(); // first get the object from the database Car carInDb = getCarById(container,disconnectedCar.getObjectId()); // copy the value-objects (int, long, double, string etc) carInDb.setName(disconnectedCar.getName()); // traverse into the references Pilot pilotInDB = carInDb.getPilot(); Pilot disconnectedPilot = disconnectedCar.getPilot(); // check if the object is still the same if(pilotInDB.getObjectId().equals(disconnectedPilot.getObjectId())){ // if it is, copy the value-objects pilotInDB.setName(disconnectedPilot.getName()); pilotInDB.setPoints(disconnectedPilot.getPoints()); } else{ // otherwise replace the object carInDb.setPilot(disconnectedPilot); } // finally store the changes container.store(pilotInDB); container.store(carInDb);
You can use reflection to automated this process. You can also use existing libraries like Dozer which help you to do this.