Skip to content

Can't update entity field to null #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RushPVA opened this issue Mar 11, 2017 · 8 comments
Closed

Can't update entity field to null #111

RushPVA opened this issue Mar 11, 2017 · 8 comments
Assignees
Milestone

Comments

@RushPVA
Copy link

RushPVA commented Mar 11, 2017

Code sample:

//insert
User user = new User();
user.setUsername("test");
user.setPassword("test");
user.setName("test");
userMapper.insert(user);

//update
user.setPassword("test2");
user.setName(null);
userMapper.update(user);

Password was updated, but name was not.

@yezhoujie
Copy link

hi:
it is the strategy when it handle update,it will not to update when a property is null, to make more simpler to update. you just need to set the property which you need to update, so that you don't need to get the whole object with full properties from db.
in your case, you should to use "" or other value to take the place of null.

@easybest easybest self-assigned this Mar 11, 2017
@easybest easybest added this to the 1.0.11 milestone Mar 11, 2017
@easybest
Copy link
Owner

  1. set name with value ""
  2. write a update statement in Mapper xml file by yourself

@RushPVA
Copy link
Author

RushPVA commented Mar 12, 2017

may be better to have choice between strategies in configuration? Because updating fields into null is the default strategy for other spring data repositories.

@RushPVA
Copy link
Author

RushPVA commented Mar 12, 2017

  1. using "" for empty field is not good aproach for me(for example it ruins not null constraint in data base, also i need additional checks - name != null && !name.isEmpty()
  2. i choose spring data to not write boiler plate code, so writing custom xml mappers for simple updates is not right way

@kopax
Copy link

kopax commented Mar 12, 2017

I must agree with @RushPVA, it's surprising this update doesn't set to null in database. Would be nice to have both choice.

@RushPVA
Copy link
Author

RushPVA commented Mar 12, 2017

and one more:
ok "" for empty string fields, but what should i set for numbers or dates?

@easybest easybest reopened this Mar 13, 2017
@easybest easybest modified the milestones: 1.0.14, 1.0.11 Mar 13, 2017
@easybest
Copy link
Owner

in 1.0.14:

update method will not ignore null property, and added two new methods:

    <S extends T> S updateIgnoreNull(S entity);

    <S extends T> S saveIgnoreNull(S entity);

These two methods will ignore null property when update.

@easybest easybest reopened this Mar 13, 2017
@RushPVA
Copy link
Author

RushPVA commented Mar 13, 2017

Welldone!!!
P.S. I am fun of mybatis, and i am glad to see that spring data mybatis project finally appeared in the developing.

easybest pushed a commit that referenced this issue Nov 12, 2019
easybest added a commit that referenced this issue Nov 8, 2021
easybest pushed a commit that referenced this issue Nov 8, 2021
easybest added a commit that referenced this issue Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants