Skip to content

Support for saving of Null values for a property [DATAMONGO-1107] #2024

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
spring-projects-issues opened this issue Nov 28, 2014 · 7 comments
Assignees
Labels
in: mapping Mapping and conversion infrastructure in: repository Repositories abstraction type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

Alexej Kubarev opened DATAMONGO-1107 and commented

Currently it is not possible to save null-values for properties of an entity.
For example if trying to save an object Person that looks like below:

@Data
class Person {
    @Id
    private ObjectId id;
    private ObjectId parentId;
    private String name;
}

With only name set, it's natural that id is auto-created later, but in some cases it's desired to save parentId: null explicitly.
Today, saving this object would result in

{
   _id: ObjectId("......."),
   _class: "....Person",
   name: "some name"
}

I can understand the reasoning as Java does not really differentiate between null and undefined but this may cause problems in interoperability between different systems that work on the same data (i know, opening a Pandora's Box here).

If a system that created a person object in a DB was using another language or native driver, it would be possible to create entries with explicit parentId property set to null. If our system, however can also create Person, then we would never be able to achieve this simple with a MongoRepository.save(Entity s); method. So on both system we would have to query for something like findByParentIdNotExistsOrIsNull().

Generally todays behavior seems appropriate, but it would be helpful if we can have some way of saying that this value is nullable and thus should be explicitly saves as null. Maybe a @Nullable annotation on a field?.

It was also pointed out that it's possible to work around this by having an event listener for the AfterConvertEvent and explicitly set this value to null if needed on the entity as MongoDB driver supports saving null values


Affects: 1.6.1 (Evans SR1)

Attachments:

5 votes, 5 watchers

@spring-projects-issues
Copy link
Author

dit commented

this is really a bug and should be solved...

@spring-projects-issues
Copy link
Author

Yura Nosenko commented

Hi, Oliver! IS anyone looking into this issue right now?

@spring-projects-issues
Copy link
Author

Yura Nosenko commented

I would be happy to help if we discuss a possible solution

@spring-projects-issues
Copy link
Author

Nilesh Akhade commented

We can also add a property if we want all our fields to be handled this way.

spring.data.mongodb.persist-null-fields=true

@spring-projects-issues
Copy link
Author

Prateek commented

@Nikhil Akhade - I don't see  spring.data.mongodb.persist-null-fields=true option showing in my spring boot application.properties. Question : https://stackoverflow.com/questions/55596980/spring-data-mongo-doesnt-persist-field-value-as-null#

@spring-projects-issues
Copy link
Author

Nilesh Akhade commented

Hey Prateek, assuming you mean @Nilesh Akhade.
This feature is not yet available with spring mongo data. I have suggested the property in my comment

@spring-projects-issues spring-projects-issues added in: repository Repositories abstraction type: enhancement A general enhancement in: mapping Mapping and conversion infrastructure labels Dec 30, 2020
@christophstrobl
Copy link
Member

Resolved by #3407

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping Mapping and conversion infrastructure in: repository Repositories abstraction type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants