Announcement Announcement Module
Collapse
No announcement yet.
[Neo4j, REST] Proper way to update entity via repository using save method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Neo4j, REST] Proper way to update entity via repository using save method

    Hi everyone! I'm a newbie in the spring framework and I want to ask for your advice. Currently, I'm creating some sort of RESTAPI application and have faced the following problem. I'm trying to update an entity in the controller. I receive entity from the request body via @RequestBody annotation and when I save this entity with repository using save method and I get overridden data. It's ok, but what about if I want to update only several fields, not all of them. When I send to my action only two properties, let's consider that as "email" and "id", the @RequestBody creates an entity with filled properties ("id" and "email"), but other properties have null values. So, when I save this entity, all existing properties in the node are overridden with these null values.

    I want to achieve some sort of partial update when I update only "dirty" properties. I would be grateful for any advice. My code bellow:

    PHP Code:
    @NodeEntity
    public class User {

      @
    GraphId Long id;
      
      
    String name;
      
    String email;
      
      public 
    Long getId() {
        return 
    id;
      }
      
      public 
    void setId(Long id) {
        
    this.id id;
      }
      
      public 
    String getName() {
        return 
    name;
      }
      
      public 
    void setName(String name) {
        
    this.name name;
      }
      
      public 
    String getEmail() {
        return 
    email;
      }
      
      public 
    void setEmail(String email) {
        
    this.email email;
      }

    PHP Code:
    public interface UserRepository extends GraphRepository<User> {
      public 
    User findById(Long id);

    PHP Code:
    @Controller
    @RequestMapping("/users")
    public class 
    UserController {

      @
    Autowired
      UserRepository r
    ;
      
      @
    RequestMapping(method RequestMethod.PUT)
      @
    ResponseStatus(HttpStatus.OK)
      public 
    void update(@RequestBody User u) {
        if (
    r.exists(u.getId())) {
          
    r.save(u);
        }
      }
      


    I have already tried to map params with MultiValueMap and then populated the entity with BeanUtils.populate() but got 415 error. It's weird because I'm using "application/json" as a content type for all my requests and I have read that @requestBody should map json automatically.

    So there is a problem and I would be grateful for any help. Thanks.

  • #2
    Please, move your question to the StackOverflow - we are going to close this forum soon and rely on SO.
    We need to clean this forum (old unanswered question) before close it.

    Thanks for understanding

    Comment

    Working...
    X