QueryDSL adds this path as a source to the query exception

Environment: Spring, JPA, Hibernate

I get this Stacktrace (class names changed):

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) 

I am trying to do this:

 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 is an object defined as PK QFoo, and the relationships were automatically determined by QueryDSQL through Maven.

I am trying to execute a query like this:

SELECT * FROM TABLE1 WHERE XCODE = 'code' AND XNAME = 'name'

TABLE1.XCODE and TABLE1.YCODE will be composite primary keys, and TABLE1.XNAME will be just a field.

I am not sure what causes the error.

+4
source share
1 answer

QFooPK.fooPK is a top-level variable and is not related to your entity. Your example should work as follows

 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)); } 

Replace id with your id property name.

+5
source

Source: https://habr.com/ru/post/1494799/


All Articles