我在QueryDSL中有以下查询:
Expression<?>[] projection = {
table.field1.as("field1"),
table.field2.as("field2")
};
SQLSubQuery subquery = new SQLSubQuery()
.from(table)
.where(...);
NumberPath<BigDecimal> field1 = new NumberPath<BigDecimal>(BigDecimal.class, "field1");
DateTimePath<Timestamp> field2 = new DateTimePath<Timestamp>(Timestamp.class, "field2");
SQLSubQuery query = new SQLSubQuery()
.from(table, subquery.list(projection))
.where(table.field1.eq(field1).and(table.field2.eq(field2)))
.groupBy(contentcache1.programId, contentcache1.id);
我试图在主查询中引用子查询(field1和field2)中的别名字段。不幸的是,我收到了这个错误:
java.lang.IllegalArgumentException: Undeclared path 'field1'. Add this path as a source to the query to be able to reference it.
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:65)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:27)
at com.mysema.query.types.path.NumberPath.accept(NumberPath.java:53)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:119)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:53)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:27)
at com.mysema.query.types.expr.BooleanOperation.accept(BooleanOperation.java:53)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:119)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:53)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:27)
at com.mysema.query.types.expr.BooleanOperation.accept(BooleanOperation.java:53)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:87)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:27)
at com.mysema.query.types.query.ListSubQuery.accept(ListSubQuery.java:69)
at com.mysema.query.DefaultQueryMetadata.validate(DefaultQueryMetadata.java:318)
at com.mysema.query.DefaultQueryMetadata.addJoin(DefaultQueryMetadata.java:112)
at com.mysema.query.DefaultQueryMetadata.addJoin(DefaultQueryMetadata.java:102)
at com.mysema.query.support.QueryMixin.from(QueryMixin.java:143)
at com.mysema.query.jpa.AbstractSQLQuery.from(AbstractSQLQuery.java:67)
at com.irtve.plataforma.api.service.dsl.impl.AssetManagerImpl.findAllFirstInProgram(AssetManagerImpl.java:466)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy51.findAllFirstInProgram(Unknown Source)
at com.irtve.plataforma.bms.model.multimedia.video.VideoRepositoryImpl.findAllFirstInProgram(VideoRepositoryImpl.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy83.findAllFirstInProgram(Unknown Source)
at com.irtve.plataforma.bms.model.multimedia.video.VideosProgramasServiceImpl.findAllFirstInProgram(VideosProgramasServiceImpl.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy105.findAllFirstInProgram(Unknown Source)
at com.irtve.plataforma.rest.service.multimedias.videosprogramas.delegate.VideosProgramasServiceDelegate.findAll(VideosProgramasServiceDelegate.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.cache.interceptor.CacheInterceptor$1.invoke(CacheInterceptor.java:58)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:213)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy195.findAll(Unknown Source)
at com.irtve.plataforma.rest.service.multimedias.videosprogramas.VideosProgramasServiceFacadeImpl.findAll(VideosProgramasServiceFacadeImpl.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.cache.interceptor.CacheInterceptor$1.invoke(CacheInterceptor.java:58)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:213)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy196.findAll(Unknown Source)
at com.irtve.plataforma.rest.service.multimedias.videosprogramas.controller.VideosProgramasController.findAll(VideosProgramasController.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at net.sf.ehcache.constructs.web.filter.CachingFilter.buildPage(CachingFilter.java:302)
at net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilter.buildPage(SimpleCachingHeadersPageCachingFilter.java:120)
at net.sf.ehcache.constructs.web.filter.CachingFilter.buildPageInfo(CachingFilter.java:247)
at net.sf.ehcache.constructs.web.filter.CachingFilter.doFilter(CachingFilter.java:216)
at com.irtve.plataforma.rest.cache.RestCacheFilter.doFilter(RestCacheFilter.java:87)
at net.sf.ehcache.constructs.web.filter.Filter.doFilter(Filter.java:86)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:167)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at com.googlecode.psiprobe.Tomcat60AgentValve.invoke(Tomcat60AgentValve.java:30)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
我还尝试过以下方法:
SSTable aliasedTable = new SSTable("AliasedTable");
SQLSubQuery subquery = new SQLSubQuery()
.from(aliasedTable)
.where(...);
NumberPath<BigDecimal> field1 = new NumberPath<BigDecimal>(BigDecimal.class, "field1");
DateTimePath<Timestamp> field2 = new DateTimePath<Timestamp>(Timestamp.class, "field2");
SQLSubQuery query = new SQLSubQuery()
.from(table, subquery.list(projection))
.where(table.field1.eq(aliasedTable.field1).and(table.field2.eq(aliasedTable.field2)))
.groupBy(contentcache1.programId, contentcache1.id);
但是我得到了同样的错误但是#34; AliasedTable&#34;而不是&#34; field1&#34;。
如何从外部选择中引用别名字段?
非常感谢您提前
答案 0 :(得分:1)
我终于使用表达式模板:
NumberExpression<BigDecimal> field1 = Expressions.numberTemplate(BigDecimal.class, "field1");
DateTimeExpression<Timestamp> field2 = DateTimeTemplate.create(Timestamp.class, "field2");
SQLSubQuery query = new SQLSubQuery()
.from(table, subquery.list(projection))
.where(table.field1.eq(field1).and(table.field2.eq(field2)))
.groupBy(contentcache1.programId, contentcache1.id);
<强>更新强>
虽然我是这样做的,但蒂莫是一个更好,更清洁的解决方案。
答案 1 :(得分:1)
这应该有效
SSTable table2 = new SSTable("table2");
SQLSubQuery subquery = new SQLSubQuery()
.from(table2)
.where(...);
SQLSubQuery query = new SQLSubQuery()
.from(table, subquery.list(projection).as(table2))
.where(table.field1.eq(table2.field1), table.field2.eq(table2.field2))
.groupBy(contentcache1.programId, contentcache1.id);