Redis Query-by-Example supports only case-sensitive matching

engourdi 2019-04-14 11:31:43
用springboot整合redis和jpa爆的错,数据库mysql
配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xx?useunicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=xx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=60
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = none
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.main.allow-bean-definition-overriding=true
# \load all-endpoint/ default info / health
management.endpoints.web.exposure.include=*
#redis
spring.redis.host=127.0.0.1
spring.redis.password=xx
spring.redis.lettuce.pool.maxActive=5
spring.redis.lettuce.pool.maxIdle=5


代码:

public interface MyRedisUserRepository extends CrudRepository<User, Long>{
Optional<User> findOneByPhone(String phone);
//Optional<User> findOneByEmail(String email);
}

public interface MysqlUserRepository extends JpaRepository<User, Long>{

}
@Slf4j
@Service
@Transactional
public class UserService {
@Autowired
MysqlUserRepository mysqlUserRepository;
@Autowired
MyRedisUserRepository myRedisUserRepository;
public Optional<User> findUserByPhone(String phone){
Optional<User> catched = myRedisUserRepository.findOneByPhone(phone);
if(catched.isPresent()) {
User user = catched.get();
log.info("user {} found in cache.",user);
return Optional.of(user);
}else {
Optional<User> raw = findOneUserByPhone(phone);
raw.ifPresent(c->{
myRedisUserRepository.save(c);
});
return raw;
}
}
public Optional<User> findOneUserByPhone(String phone){
ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("phone", exact().ignoreCase());
Optional<User> user = mysqlUserRepository.findOne(Example.of(User.builder().phone(phone).build(),matcher));
log.info("user found in sql:{}",user);
return user;
}
public Optional<User> saveUser2Sql(User user){
user = mysqlUserRepository.save(user);
return Optional.of(user);
}
}
@SpringBootApplication
@Slf4j
@EnableJpaRepositories
@EnableRedisRepositories
public class MybootApplication implements ApplicationRunner{
@Autowired
private UserService userService;
public static void main(String[] args) {
SpringApplication.run(MybootApplication.class, args);
}

@Override
public void run(ApplicationArguments args) throws Exception {
// Optional<User> u = userService.findUserByPhone("xxxx");
// log.info("user {}",u);
// userService.findAllUser();
User user = User.builder().phone("xxxxx").name("xx").city("xx").email("xx@126.com").build();
// userService.saveUser(user);
// userService.findUserByPhone("xxx");
log.info("user {}",user);
userService.saveUser2Sql(user);
log.info("user {}",user);
log.info("user {}",userService.findOneUserByPhone("xxxx").get());
//userService.findUserByEmail("engourdi@126.com");
}

}

@Entity
@Builder
@RedisHash(value = "myboot-user", timeToLive = 2592000)
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "user")
public @Data class User implements Serializable{
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Integer id;
private String name;
private String phone;
private String email;
private String password;
}

异常日志:

java.lang.IllegalStateException: Failed to execute ApplicationRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:807) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:794) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at com.franklin.myboot.MybootApplication.main(MybootApplication.java:28) [classes/:na]
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Redis Query-by-Example supports only case-sensitive matching.
at org.springframework.data.redis.repository.query.ExampleQueryMapper.applyPropertySpec(ExampleQueryMapper.java:137) ~[spring-data-redis-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.data.redis.repository.query.ExampleQueryMapper.lambda$applyPropertySpecs$0(ExampleQueryMapper.java:118) ~[spring-data-redis-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at java.lang.Iterable.forEach(Unknown Source) ~[na:1.8.0_161]
at org.springframework.data.redis.repository.query.ExampleQueryMapper.applyPropertySpecs(ExampleQueryMapper.java:106) ~[spring-data-redis-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.data.redis.repository.query.ExampleQueryMapper.getMappedExample(ExampleQueryMapper.java:88) ~[spring-data-redis-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.data.redis.repository.support.QueryByExampleRedisExecutor.createQuery(QueryByExampleRedisExecutor.java:187) ~[spring-data-redis-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.data.redis.repository.support.QueryByExampleRedisExecutor.findOne(QueryByExampleRedisExecutor.java:97) ~[spring-data-redis-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_161]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at com.sun.proxy.$Proxy113.findOne(Unknown Source) ~[na:na]
at com.franklin.myboot.service.UserService.findOneUserByPhone(UserService.java:55) ~[classes/:na]
at com.franklin.myboot.service.UserService$$FastClassBySpringCGLIB$$322924e3.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.spri
...全文
148 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
engourdi 2019-04-14
  • 打赏
  • 举报
回复
自己蠢了,把springcache的标注和jpa的混在一起了,改了以后就ok了

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧