原因:java.lang.IllegalArgumentException:包含(1):Redis查询派生不支持[IsContaining,Containing,Contains]-Redis

时间:2018-11-03 19:03:34

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

我正在开发Spring Boot + Redis示例。在此示例中,我开发了一些自定义方法,这些方法基于RoleName提取详细信息。对于以下方法userRepository.findByRole_RoleName("ADMIN")userRepository.findByMiddleNameContaining("Li");,我们得到以下异常。

参考网址:https://docs.spring.io/spring-data/keyvalue/docs/1.2.15.RELEASE/reference/html/

有人可以请提供程序的指针吗?所有其他方法都可以正常工作。但是只是这种方法引起了问题。我将在下面发布所有必需的代码以供参考。

错误:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:776)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)
    at com.baeldung.MainAppDemo.main(MainAppDemo.java:21)
Caused by: java.lang.IllegalArgumentException: CONTAINING (1): [IsContaining, Containing, Contains]is not supported for redis query derivation
    at org.springframework.data.redis.repository.query.RedisQueryCreator.from(RedisQueryCreator.java:67)
    at org.springframework.data.redis.repository.query.RedisQueryCreator.create(RedisQueryCreator.java:53)
    at org.springframework.data.redis.repository.query.RedisQueryCreator.create(RedisQueryCreator.java:41)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:119)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:95)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:81)
    at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.createQuery(KeyValuePartTreeQuery.java:211)
    at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.prepareQuery(KeyValuePartTreeQuery.java:148)
    at org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.execute(KeyValuePartTreeQuery.java:106)
    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 org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy65.findByMiddleNameContains(Unknown Source)
    at com.baeldung.MainAppDemo.run(MainAppDemo.java:38)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:792)
    ... 5 common frames omitted
2018-11-04 00:27:29,639 INFO  [main] org.springframework.context.support.AbstractApplicationContext: Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6f96c77: startup date [Sun Nov 04 00:27:26 IST 2018]; root of context hierarchy
2018-11-04 00:27:29,645 INFO  [main] org.springframework.jmx.export.MBeanExporter: Unregistering JMX-exposed beans on shutdown

User.java

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("user")
public class User {
    private @Id String id;
    private @Indexed String firstName;
    private @Indexed String middleName;
    private @Indexed String lastName;
    private Role role;
}

Role.java

@Data 
@Builder
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("Role")
public class Role {
    private @Id String id;
    private @Indexed String roleName;
}

UserRepository.java

public interface UserRepository extends CrudRepository<User, String>{
    List<User> findByFirstNameAndLastName(String firstName, String lastName);

    List<User> findByMiddleNameContains(String firstName);

    List<User> findByRole_RoleName(String roleName);
}

MainAppDemo.java

@SpringBootApplication
public class MainAppDemo implements CommandLineRunner{

    @Autowired
    private UserRepository userRepository;

    public static void main(String[] args) {
        SpringApplication.run(MainAppDemo.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Role role1 = Role.builder().id("R1").roleName("ADMIN").build();
        User user1 = User.builder().firstName("Matt").middleName("Mike").lastName("Wixson").role(role1).build();

        Role role2 = Role.builder().id("R2").roleName("API").build();
        User user2 = User.builder().firstName("John").middleName("Lima").lastName("Kerr").role(role2).build();

        userRepository.save(user1);
        userRepository.save(user2);

        List<User> u = userRepository.findByFirstNameAndLastName("Matt", "Wixson");
        System.out.println("Find By First Name and Last Name = "+u.toString());

        List<User> u2 = userRepository.findByMiddleNameContains("Li");
        System.out.println("Contains ="+u2);

        List<User> adminUser = userRepository.findByRole_RoleName("ADMIN");
        System.out.println("ADMIN USER ="+adminUser);
    }
}

JIRA缺陷:https://jira.spring.io/browse/DATAREDIS-887

更新:

我开发了这样的查询并从main方法调用,但仍然遇到相同的错误。请提出可行的解决方案。

@Query("SELECT u FROM User u WHERE u.middleName LIKE :middleName ")
List<User> findByMiddleNameContaining(@Param("middleName") String middleName); 

2 个答案:

答案 0 :(得分:1)

好让我们从“ Redis的工作原理”开始

Redis使用ID哈希进行工作,这有助于更快地定位记录。 还对@Indexed进行了哈希处理和存储,以更快地精确定位记录

因此,默认情况下,对于MiddleName,“包含”查询将不起作用,因为“测试”字符串的哈希将不包含在字符串“ TestUser”的哈希中。

但是 ExampleMatcher 在这里是要救援的。

来源:https://docs.spring.io/spring-data/redis/docs/2.1.2.RELEASE/reference/html/#query-by-example

在角色对象上搜索RoleName的解决方案相对简单:使用此查询

userRepository.findByRoleRoleName(“ ADMIN”) (基本上删除下划线)

好消息是它可以与上述 ExampleMatcher 结合使用。

如果您有疑问,请讨论。

RoleName搜索的帮助完整参考:Query Nested Objects in Redis using Spring Data

答案 1 :(得分:0)

在带有Spring Boot的Redis上,您只需要查询一些支持finder的查询,例如Is,Equals,And和Or,您可以查看表9:

https://docs.spring.io/spring-data/redis/docs/current/reference/html/#redis.repositories.queries

已更新

您可以在此文档链接之后创建查询作为参考

https://docs.spring.io/spring-data/redis/docs/2.1.2.RELEASE/reference/html/#query-by-example