将 QuerydslPredicateExecutor 与 JpaRepository 一起使用时出现 IllegalAccessException
Posted
技术标签:
【中文标题】将 QuerydslPredicateExecutor 与 JpaRepository 一起使用时出现 IllegalAccessException【英文标题】:IllegalAccessException when using QuerydslPredicateExecutor with JpaRepository 【发布时间】:2022-01-04 02:45:41 【问题描述】:应用:
kotlin 1.6 jdk 17(也试过 8 和 11) gradle 7.3 spring-boot 2.6.0 spring-data-jpa 查询DSL 5.0 jpa 科特林 适合 kotlin-codegen我正在尝试将QuerydslPredicateExecutor
与JpaRepository
一起使用,但是当应用程序启动时,它无法使用IllegalStateException
进行初始化,因为它无法访问生成的Q 类的某些字段或方法.
使用JPAQuery
代替QuerydslPredicateExecutor
存储库虽然有效。
任何关于潜在问题的想法都将不胜感激。
实体
@Entity
class Workplace(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,
var name: String,
var description: String,
var location: String,
)
存储库:
@Repository
interface WorkplaceRepository : JpaRepository<Workplace, Long>, QuerydslPredicateExecutor<Workplace>
服务:
@Service
class WorkplaceService(
private val workplaceRepository: WorkplaceRepository,
)
fun findAll(searchOpts: SearchOpts): Iterable<Workplace>
return workplaceRepository.findAll(searchOpts.toPredicate())
异常失败:
Caused by: java.lang.IllegalStateException: Could not access method or field: class org.springframework.util.ReflectionUtils cannot access a member of class <package>.QWorkplace with modifiers "private static final"
at org.springframework.util.ReflectionUtils.handleReflectionException(ReflectionUtils.java:107) ~[spring-core-5.3.13.jar:5.3.13]
at org.springframework.util.ReflectionUtils.getField(ReflectionUtils.java:657) ~[spring-core-5.3.13.jar:5.3.13]
at org.springframework.data.querydsl.SimpleEntityPathResolver.lambda$createPath$0(SimpleEntityPathResolver.java:74) ~[spring-data-commons-2.6.0.jar:2.6.0]
at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
at org.springframework.data.querydsl.SimpleEntityPathResolver.createPath(SimpleEntityPathResolver.java:74) ~[spring-data-commons-2.6.0.jar:2.6.0]
at org.springframework.data.jpa.repository.support.QuerydslJpaPredicateExecutor.<init>(QuerydslJpaPredicateExecutor.java:81) ~[spring-data-jpa-2.6.0.jar:2.6.0]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getRepositoryFragments(JpaRepositoryFactory.java:272) ~[spring-data-jpa-2.6.0.jar:2.6.0]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getRepositoryFragments(JpaRepositoryFactory.java:242) ~[spring-data-jpa-2.6.0.jar:2.6.0]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepositoryComposition(RepositoryFactorySupport.java:434) ~[spring-data-commons-2.6.0.jar:2.6.0]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:300) ~[spring-data-commons-2.6.0.jar:2.6.0]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:322) ~[spring-data-commons-2.6.0.jar:2.6.0]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:230) ~[spring-data-commons-2.6.0.jar:2.6.0]
at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-2.6.0.jar:2.6.0]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:328) ~[spring-data-commons-2.6.0.jar:2.6.0]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.6.0.jar:2.6.0]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.13.jar:5.3.13]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.13.jar:5.3.13]
... 179 common frames omitted
但这是可行的:
存储库:
@Repository
interface WorkplaceRepository : JpaRepository<Workplace, Long>
服务
@Service
class WorkplaceService(
private val workplaceRepository: WorkplaceRepository,
private val entityManager: EntityManager,
)
fun findAll(searchOpts: SearchOpts): Iterable<Workplace>
return JPAQuery<Workplace>(entityManager)
.from(QWorkplace.workplace)
.where(searchOpts.toPredicate())
.fetch()
【问题讨论】:
【参考方案1】:我认为这与在 https://github.com/querydsl/querydsl/pull/3076 中修复但尚未发布的错误有关。
【讨论】:
感谢您的快速回复!这似乎是它。你知道下一个版本什么时候发布吗?以上是关于将 QuerydslPredicateExecutor 与 JpaRepository 一起使用时出现 IllegalAccessException的主要内容,如果未能解决你的问题,请参考以下文章
Javascript 将正则表达式 \\n 替换为 \n,将 \\t 替换为 \t,将 \\r 替换为 \r 等等