我正在开发Spring Boot + Spring Data Redis
示例。在此示例中-我以嵌套方式对redis
进行了建模。当我只是查询部门的详细信息时,我什么也没得到。如何解决这个问题?
错误:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.example.model.Employee] to type [com.example.model.Department]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:293)
at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.lambda$and$0(ResultProcessor.java:213)
at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:224)
at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:152)
at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:119)
at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.execute(KeyValuePartTreeQuery.java:109)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:602)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:590)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at
Employee.java
@RedisHash("employees")
public class Employee {
@Id @Indexed
private String id;
private String firstName;
private String lastName;
private List<Address> addresses;
private List<Department> departments;
}
Department.java
@RedisHash("departments")
public class Department {
@Id
private String id;
@Indexed
private String departmentName;
private List<Project> projects;
}
Project.java
public class Project {
@Indexed
private String projectName;
@Indexed
private String projectCategory;
}
EmployeeRepository.java
public interface EmployeeRepository extends CrudRepository<Employee, String>{
List<Employee> findByAddresses_Street(String street);
List<Employee> findByAddresses_City(String city);
List<Employee> findByDepartments_Projects_ProjectCategory(String category);
List<Department> findByDepartments_DepartmentName(String departmentNames);
}
EmployeeDeparmentProjectsTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class EmployeeDeparmentProjectsTest extends RepositoryTestSupport{
@Autowired private EmployeeRepository employeeRepository;
@Autowired private DepartmentsRepository departmentsRepository;
//@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();
// Projects
Project linkedIn = Project.builder().projectName("Linked-in").projectCategory("Social").build();
Project stack = Project.builder().projectName("StackOverFlow").projectCategory("Developer-Social").build();
Project electrical = Project.builder().projectName("Electrical").projectCategory("Electric").build();
Project mechanical = Project.builder().projectName("Mechanical").projectCategory("Machines").build();
Project ml = Project.builder().projectName("ML").projectCategory("Social").build();
// Departments
Department ai = Department.builder().departmentName("AI").projects(Arrays.asList(linkedIn, stack)).build();
Department nonAI = Department.builder().departmentName("NON-AI").projects(Arrays.asList(electrical, mechanical)).build();
Department dml = Department.builder().departmentName("ML").projects(Arrays.asList(ml)).build();
// Employee Details
Employee raj = Employee.builder().firstName("Raj").lastName("Kumar")
.addresses(Arrays.asList(home, offc))
.departments(Arrays.asList(ai, nonAI)).build();
Employee john = Employee.builder().firstName("John").lastName("Doe")
.addresses(Arrays.asList(home, offc))
.departments(Arrays.asList(dml)).build();
employeeRepository.save(raj);
employeeRepository.save(john);
List<Employee> employees = employeeRepository.findByAddresses_Street("XYZ Street");
System.out.println("EMPLOYEE = "+employees);
}
@Test
public void test() {
List<Employee> employees = employeeRepository.findByDepartments_Projects_ProjectCategory("Social");
System.out.println("EMployees : "+employees);
List<Department> departments = departmentsRepository.findByDepartmentName("AI");
System.out.println("Departments = "+departments);
List<Department> myDepartments = employeeRepository.findByDepartments_DepartmentName("AI");
System.out.println("DEPARTMENT SIZE ? ="+myDepartments.size());
System.out.println("DEPARTMENTS = "+myDepartments);
}
}
?
答案 0 :(得分:0)
由于关系的定义类似于嵌套。以下命令不起作用
List<Department> findByDepartments_DepartmentName(String departmentNames);
这是我们如何获取数据的方法
List<Employee> employees_1 = employeeRepository.findByDepartments_DepartmentName("AI");
// Get Departments
List<Department> list = new ArrayList<>();
for (Employee employee : employees_1) {
System.out.println("EMP ? = "+employee.getDepartments());
list.addAll(employee.getDepartments());
}
System.out.println(list);