How can I ensure CassandraOperations.selectOneById() initializes all fields in the POJO?

房东的猫 提交于 2019-12-02 13:00:02

In short

Cassandra stores empty collections as null and Spring Data Cassandra overwrites initialized fields.

Explanation

Cassandra list/set typed columns represent an empty collection as null. It does not matter whether the list/set (as viewed from Java/Groovy) was empty or null. Storing an empty list yields therefore in null. From here one can't tell whether the state was null or empty at the time saving the value.

Spring Data Cassandra overwrites all fields with values retrieved from the result set and so your pre-initialized fields is set to null. I created a ticket DATACASS-266 to track the state of this issue.

Workaround

Spring Data uses setters if possible so you have a chance to intervene. A very simple null guard could be:

public void setMyList(List<Long> myList) { if(myList == null){ this.myList = new ArrayList<>(); return; } this.myList = myList; }

As important addition to mp911de answer you have to set @org.springframework.data.annotation.AccessType(AccessType.Type.PROPERTY) to make this solution work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!