Spring Data Redis不会将Date保留为空值

时间:2018-12-11 09:16:34

标签: spring spring-boot redis spring-data-redis

我是 Spring Data Redis 的新手,正在尝试将deletedDate保存为null。当我在Redis中持久保存deletedDate时,它不持久为null。我正在使用Java8 LocalDateTime格式化程序。

User.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("users")
public class User {
    @Id @Indexed
    private String userId;
    private String name;
    private LocalDate createdDate;
}

MyTestclasses:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserGroupTest extends RepositoryTestSupport{
    @Autowired private UserRepository userRepository;
    @Autowired private GroupRepository groupRepository;
    @Autowired private UserGroupRelRepository userGroupRelRepository;

    @Before
    public void setUp() throws JsonProcessingException {
        User raj = User.builder().name("Raj Kumar").createdDate(null).build();
        User parag = User.builder().name("Parag Rane").createdDate(null).build();
        User sagar = User.builder().name("Sagar Parate").createdDate(null).build();

        userRepository.saveAll(Arrays.asList(raj, parag, sagar));

        Group hardware = Group.builder().name("Hardware").build();
        Group software = Group.builder().name("Hardware").build();
        Group machineLearning = Group.builder().name("Mchine Learning").build();
        Group ai = Group.builder().name("Artificial Inteligence").build();

        groupRepository.saveAll(Arrays.asList(hardware, software, machineLearning));


        // Raj interested in Hardware and AI
        UserGroupRel userGroupRel1 = UserGroupRel.builder().userId(raj.getUserId()).groupId(hardware.getGroupId()).build();
        UserGroupRel userGroupRel2 = UserGroupRel.builder().userId(raj.getUserId()).groupId(ai.getGroupId()).build();

        // Parag interested in hardware, software and ML
        UserGroupRel userGroupRel_1 = UserGroupRel.builder().userId(parag.getUserId()).groupId(hardware.getGroupId()).build();
        UserGroupRel userGroupRel_2 = UserGroupRel.builder().userId(parag.getUserId()).groupId(software.getGroupId()).build();
        UserGroupRel userGroupRel_3 = UserGroupRel.builder().userId(parag.getUserId()).groupId(machineLearning.getGroupId()).build();

        // Sagar intersted in AI and ML
        UserGroupRel user_GroupRel1 = UserGroupRel.builder().userId(sagar.getUserId()).groupId(machineLearning.getGroupId()).build();
        UserGroupRel user_GroupRel2 = UserGroupRel.builder().userId(sagar.getUserId()).groupId(ai.getGroupId()).build();

        userGroupRelRepository.saveAll(Arrays.asList(userGroupRel1, userGroupRel2, userGroupRel_1, userGroupRel_2, 
                userGroupRel_3, user_GroupRel1, user_GroupRel2));


        List<UserGroupRel> userGroupRels = userGroupRelRepository.findByGroupId(hardware.getGroupId());
        System.out.println("USER GROUPS DETAILS SIZE :"+userGroupRels.size());
        for (UserGroupRel userGroupRel : userGroupRels) {
            System.out.println("USER_GROUP :"+new ObjectMapper().writeValueAsString(userGroupRel));
            List<User> users = userRepository.getByUserId(userGroupRel.getUserId());
            for(User u : users) {
                System.out.println("Users interested in Hardwares are : "+u.getName());
            }
        }
    }
}

日期另存为 enter image description here

问题已发布在这里:https://jira.spring.io/browse/DATAREDIS-912

我看到了https://github.com/facebook/hhvm/issues/6062https://github.com/NodeRedis/node_redis/issues/507,但是就Spring Data Redis的实现而言,事情还不清楚。

1 个答案:

答案 0 :(得分:0)

Spring Data Redis存储库将具有null值的属性表示为非书面哈希条目。或简化:null属性不会写入Redis。