4

环境: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 只是一个字段。

我不确定是什么导致了错误。

4

1 回答 1

5

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 属性名称。

于 2013-08-01T20:17:31.687 回答