spring-data-cassandra icon indicating copy to clipboard operation
spring-data-cassandra copied to clipboard

Loading empty collection-typed properties overwrites pre-initialized fields [DATACASS-266]

Open spring-projects-issues opened this issue 9 years ago • 3 comments

Mark Paluch opened DATACASS-266 and commented

Cassandra list/set typed columns represent an empty collection as null. From here one can't tell whether the state was null or empty at the time saving the value. Loading this value causes MappingCassandraConverter to overwrite pre-initialized fields (e.g. with new ArrayList) with null.


Affects: 1.3.4 (Gosling SR4)

Reference URL: http://stackoverflow.com/questions/36349896/how-can-i-ensure-cassandraoperations-selectonebyid-initializes-all-fields-in-t

1 votes, 4 watchers

spring-projects-issues avatar Apr 04 '16 07:04 spring-projects-issues

Paweł Stawicki commented

Some solution could be to set the property via setter, not directly setting an entity field. This way null could be handled in setter by entity author

spring-projects-issues avatar Aug 03 '18 09:08 spring-projects-issues

Mark Paluch commented

You can achieve property accessor use by specifying @AccessType(PROPERTY) on class/property level

spring-projects-issues avatar Aug 03 '18 11:08 spring-projects-issues

Scott Lehman commented

There is also the option of handling the null in a constructor that Spring Data uses to instantiate the model.  The reference guide actually recommends using an all argument constructor, and if you make the model immutable (also a recommendation), there's no need to also provide a setter.

A constructor approach does require a bit more care, so it's important to fully understand the requirements documented in the reference guide (see the "Object Mapping Fundamentals" section), and you can't simply rely on Lombok's @AllArgsConstructor, which makes the property access approach a bit simpler in terms of implementation/maintenance

spring-projects-issues avatar Jul 10 '19 16:07 spring-projects-issues