0

Supposed I have a code that update the table using the JpaRepository package

public User test(Long userId, UserDto userDto) {
    User user = findByUserId(userId);

    User mappedUser = modelMapper.map(userDto, User.class);

    if (mappedUser.getAddress() != null) {
        user.setAddress(mappedUser.getAddress());
    }

    if (mappedUser.getContact() != null) {
        user.setContact(mappedUser.getContact());
    }

    ... // more checking if != null

    return userRepository.saveAndFlush(user);
}

Is there a way where I can avoid the multiple checking of != null that uses the if statement? before saving it to the database? Thanks in advance Im new in using spring boot

Beginner
  • 1,700
  • 4
  • 22
  • 42
  • "Move it"? add a `setFromMappedUser(User mapped)` to your User class, and make _that_ responsible for setting values if not null, so that the checks happen where they should be? – Mike 'Pomax' Kamermans Dec 03 '19 at 07:20
  • How about just `user.setAddress(mappedUser.getAddress());`? Why keep the previous address or contact if the one you're received in UserDto is null? – JB Nizet Dec 03 '19 at 07:24
  • @JBNizet then it will update the entity with null values if i use ``user.setAddress(mappedUser.getAddress());`` – Beginner Dec 03 '19 at 07:26
  • Yes, and that's normally what you want. Again, if the UserDTO class has an address, used to tell what the address of a user is, and the caller calls the method with a UserDTO having a null address, why do you ignore its request to set it to null, and keep the old address instead? – JB Nizet Dec 03 '19 at 07:31
  • Take a look to this solution https://stackoverflow.com/questions/1301697/helper-in-order-to-copy-non-null-properties-from-object-to-another-java – Javier Sainz Dec 03 '19 at 07:34
  • thats not what I want I dont want to update the database with null values if the field is not change. for example i have this request { "address": "asdasd", "contact": "asdasd", } // this can update both address and contact with respective values { "address": "asdasd", } // but this can update the address with "asdasd" and also contact with null value i dont want the contact to be null i want it to just retain the old value – Beginner Dec 03 '19 at 07:35
  • So the model User will not support nullable fields? I'm asking that because if you have null values that came from the db, you have to allow the model to hold that and save back. If the values are coming from other sources (UI) you have to validate there. – Antal Attila Dec 03 '19 at 11:42

3 Answers3

0

As one in the comments mentioned move this method to your User.class.

Alternatively, I guess the User is coming from a REST Endpoint? You can define via Annotation that some fields require to be filled with

@NotBlank("error message")
@NotNull("error message")

With this approach you can be sure only users are coming in with filled data.

Markus G.
  • 1,620
  • 2
  • 25
  • 49
0

You can refer to mapstruct mapper for mapping objects. It has update method too. You can refer this link. https://mapstruct.org/

It directly maps without needing to write getter and setter. And it is one of the good practices to use in the industry.

0

When you define the UserDto, add the validations there like below

public class UserDto{
@NotNull
String address;
}

When you call the method, add the @Valid annotation

public User test(Long userId, @Valid UserDto userDto) {}

Don't forget to add the @validated annotation in the class where you defined the test method