java – QueryDSL将此路径添加为查询异常的源

环境:Spring,JPA,Hibernate

我得到这个Stacktrace(类名修改):

Undeclared path 'fooPK'. Add this path as a source to the query to be able to reference it.; nested exception is java.lang.IllegalArgumentException: Undeclared path 'fooPK'. Add this path as a source to the query to be able to reference it.
org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:293)
org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:106)
org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
com.sun.proxy.$Proxy65.findOne(Unknown Source)
com.adc.common.config.service.XServiceImpl.getByConfigName(XServiceImpl.java:49)

我想这样做:

public static final Predicate getByCodeAndName(
        final String sCode,
        final String sName) {

    BooleanExpression expression = null;
    QFooPK fooPK = QFooPK.fooPK;
    QFoo foo = QFoo.foo;

    expression = fooPK.code.eq(sCode)
            .and(foo.name.eq(sName));

    return expression;
}

QFooPK是一个被定义为QFoo PK的对象,
并且这些关系已由QueryDSQL通过Maven自动定义.

我正在尝试实现这样的查询:

SELECT * FROM TABLE1 WHERE XCODE = ‘code’ AND XNAME = ‘name’

TABLE1.XCODE和TABLE1.YCODE将是复合主键,TABLE1.XNAME只是一个字段.

我不确定导致错误的是什么.

最佳答案
QFooPK.fooPK是顶级变量,与您的实体无关.你的例子应该像这样工作

public static final Predicate getByCodeAndName(
    final String sCode,
    final String sName) {
    QFoo foo = QFooVal.foo;
    return foo.id.code.eq(sCode)
        .and(foo.name.eq(sName));
}

将id替换为您的id属性名称.

转载注明原文:java – QueryDSL将此路径添加为查询异常的源 - 代码日志