Make an update SpringData changed the POJO field

I am using Spring Data over MongoDB. I was able to save POJO, update them. It works great. But now I want to dump in db only changed POJO fields.

For example, I have a custom object. I create a user and then update lastActiveDate from time to time.

@Document
class User {
    @Id
    BigInteger ID;

    String email;

    String name;

    Date lastActiveDate;
}

User user = new User();
user.setName("User");
user.setEmail("example@example.com");
repository.save(user);

User toUpdUser = repository.findOne(userId);
toUpdUser.setLastActiveDate(new Date);
repository.save(toUpdUser );

In the second save, I want to update only the lastActiveDate field of the whole user, because updating documents can slow down on large objects. I also want to know a set of changes (at least a set of updated fields).

API . , ( ), . - beans , IFAIK Spring Data POJO Spring bean , POJO AOP POJO.

UPD:   mongo api ( mongo-like api). , . . - 5 0,2

, POJO-mappers CRUD, , . SpringData.

+7
4

Spring-data , , , . , , - PATCH PUT. $set , .

MongoConverters, PATCH -webservice spring-mvc, "events" :

@RestController
public class MyWebservice {

    @Autowired
    private MongoConverter mongoConverter;

    @RequestMapping(value = "/order/{id}", method = PATCH, produces = APPLICATION_JSON_UTF8_VALUE, consumes = APPLICATION_JSON_UTF8_VALUE)
    public Order updateOrder(@PathVariable("id") String id, @RequestBody Order order) throws JsonProcessingException {
        order.setId(id);
        DBObject update = getDbObject(order);
        mongoTemplate.updateFirst(query(where("id").is(id)), Update.fromDBObject(new BasicDBObject("$set", update)).push("events", order), Order.class);
        return mongoTemplate.findOne(query(where("id").is(id)), Order.class);
    }

    private DBObject getDbObject(Object o) {
        BasicDBObject basicDBObject = new BasicDBObject();
        mongoConverter.write(o, basicDBObject);
        return basicDBObject;
    }
}

MongoConverter BasicDBObject.

+5

API Jackson , POJO.

public class TestJacksonUpdate {

class Person implements Serializable {
    private static final long serialVersionUID = -7207591780123645266L;
    public String code = "1000";
    public String firstNm = "John";
    public String lastNm;
    public Integer age;
    public String comments = "Old Comments";

    @Override
    public String toString() {
        return "Person [code=" + code + ", firstNm=" + firstNm + ", lastNm=" + lastNm + ", age=" + age
                + ", comments=" + comments + "]";
    }
}

public static void main(String[] args) throws JsonProcessingException, IOException {
    TestJacksonUpdate o = new TestJacksonUpdate();

    String input = "{\"code\":\"10000\",\"lastNm\":\"Smith\",\"comments\":\"Jackson Update WOW\"}";
    Person persist = o.new Person();

    System.out.println("persist: " + persist);

    ObjectMapper mapper = new ObjectMapper();
    Person finalPerson = mapper.readerForUpdating(persist).readValue(input);

    System.out.println("Final: " + finalPerson);
    // repository.save(finalPerson);    } }
+1

To update individual fields of an object, use the API Updateas described in the reference documentation . Uses MongoTemplatefor this can then be included in the repository using a mechanism to implement individual query methods, as described here.

-1
source

Source: https://habr.com/ru/post/1607865/


All Articles