【问题标题】:Redis @Reference does not work in Spring Data RedisRedis @Reference 在 Spring Data Redis 中不起作用
【发布时间】:2018-11-20 05:11:54
【问题描述】:

我在Spring Boot + Spring Data Redis 中实现@Reference 时遇到问题。 AddressEmployee 中的一个列表,当我保存officehome 地址时,我希望数据与Employee 一起保存。但是数据没有被保存,因此无法使用street 搜索Address

Employee.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("employees")
public class Employee {
    @Id @Indexed
    private String id;
    private String firstName;
    private String lastName;

    @Reference
    private List<Address> addresses;
} 

地址.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("address")
public class Address {
    @Id
    private String id;
    @Indexed
    private String street;
    private String city;
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class EmployeeAdressTest extends RepositoryTestSupport{
    @Autowired private EmployeeRepository employeeRepository;


    @Before
    public void setUp() throws JsonProcessingException {
        Address home = Address.builder().street("ABC Street").city("Pune").build();
        Address offc = Address.builder().street("XYZ Street").city("Pune").build();

        Employee employee1 = Employee.builder().firstName("Raj").lastName("Kumar").addresses(Arrays.asList(home, offc)).build();
        employeeRepository.save(employee1);


        List<Employee> employees = employeeRepository.findByAddresses_Street("XYZ Street");
        System.out.println("EMPLOYEE = "+employees);
    }

    @Test
    public void test() {

    }
}

春季文档:

8.8. Persisting References
Marking properties with @Reference allows storing a simple key reference instead of copying values into the hash itself. On loading from Redis, references are resolved automatically and mapped back into the object, as shown in the following example:

Example 30. Sample Property Reference
_class = org.example.Person
id = e2c7dcee-b8cd-4424-883e-736ce564363e
firstname = rand
lastname = al’thor
mother = people:a9d4b3a0-50d3-4538-a2fc-f7fc2581ee56      
Reference stores the whole key (keyspace:id) of the referenced object.

?

【问题讨论】:

    标签: spring redis jedis spring-data-redis


    【解决方案1】:

    Spring Data Redis 要求您将存储在homeoffice 中的对象与引用对象employee1 分开保存。

    这(现在)在第 8.8 章末尾的官方文档中说明:https://docs.spring.io/spring-data-redis/docs/current/reference/html/#redis.repositories.references

    因此,如果您在保存employee1 之前将homeoffice 保存到数据库中,您应该没问题。

    同样的 btw 适用于您稍后对引用对象所做的更新。仅保存引用对象并不会保存引用对象的更新。

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 2016-05-31
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 2016-03-18
      • 2020-05-10
      • 2018-06-15
      相关资源
      最近更新 更多