【问题标题】:Spring Data Redis doesn't persist Date as null valueSpring Data Redis 不会将 Date 保留为空值
【发布时间】:2018-12-11 09:16:33
【问题描述】:

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

用户.java

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

我的测试类:

@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());
            }
        }
    }
}

另存为的日期

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

我看到了https://github.com/facebook/hhvm/issues/6062https://github.com/NodeRedis/node_redis/issues/507,但在 Spring Data Redis 中的实现还不清楚。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 2018-12-11
      相关资源
      最近更新 更多