带有where子句的HQL查询

时间:2010-02-26 08:48:46

标签: hibernate hql

我正在尝试查询给定方案的hibernate:

我的数据模型

班级沟通:

变量:subject,sentTo,creator。

现在假设在usera和userb之间交换了一些注释。我想通过hibernate查询找到所有这些注释。我在下面写了我的查询字符串。

select note from Communication n where n.subject=:subject and ((n.sentTo=:sentto and n.creator=:creator) or (n.sentTo:creator and n.creator=:sentto))

但是hibernate无法解析此查询。请建议做什么?

解析器抛出

的异常
2010-02-26 15:34:37,169 DEBUG [ErrorCounter] line 1:114: unexpected token: :
line 1:114: unexpected token: :
 at org.hibernate.hql.antlr.HqlBaseParser.atom(HqlBaseParser.java:3520)
 at org.hibernate.hql.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3216)
 at org.hibernate.hql.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3098)
 at org.hibernate.hql.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2818)
 at org.hibernate.hql.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:570)
 at org.hibernate.hql.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2586)
 at org.hibernate.hql.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2449)
 at org.hibernate.hql.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2413)
 at org.hibernate.hql.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2331)
 at org.hibernate.hql.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2296)
 at org.hibernate.hql.antlr.HqlBaseParser.expression(HqlBaseParser.java:2082)
 at org.hibernate.hql.antlr.HqlBaseParser.expressionOrVector(HqlBaseParser.java:4181)
 at org.hibernate.hql.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:944)
 at org.hibernate.hql.antlr.HqlBaseParser.atom(HqlBaseParser.java:3438)
 at org.hibernate.hql.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3216)
 at org.hibernate.hql.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3098)
 at org.hibernate.hql.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2818)
 at org.hibernate.hql.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:570)
 at org.hibernate.hql.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2586)
 at org.hibernate.hql.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2449)
 at org.hibernate.hql.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2413)
 at org.hibernate.hql.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2331)
 at org.hibernate.hql.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2306)
 at org.hibernate.hql.antlr.HqlBaseParser.expression(HqlBaseParser.java:2082)
 at org.hibernate.hql.antlr.HqlBaseParser.expressionOrVector(HqlBaseParser.java:4181)
 at org.hibernate.hql.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:944)
 at org.hibernate.hql.antlr.HqlBaseParser.atom(HqlBaseParser.java:3438)
 at org.hibernate.hql.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3216)
 at org.hibernate.hql.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3098)
 at org.hibernate.hql.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2818)
 at org.hibernate.hql.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:570)
 at org.hibernate.hql.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2586)
 at org.hibernate.hql.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2449)
 at org.hibernate.hql.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2413)
 at org.hibernate.hql.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2341)
 at org.hibernate.hql.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2296)
 at org.hibernate.hql.antlr.HqlBaseParser.expression(HqlBaseParser.java:2082)
 at org.hibernate.hql.antlr.HqlBaseParser.logicalExpression(HqlBaseParser.java:1858)
 at org.hibernate.hql.antlr.HqlBaseParser.whereClause(HqlBaseParser.java:454)
 at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:708)
 at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
 at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
 at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:271)
 at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)
 at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
 at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
 at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
 at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
 at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:436)
 at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:384)
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
 at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
 at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:871)
 at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:797)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1460)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1398)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1305)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
 at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:261)
 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:192)
 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)
 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
 at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
 at org.apache.catalina.core.StandardService.start(StandardService.java:516)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:593)
 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.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

3 个答案:

答案 0 :(得分:2)

尝试使用:

select note from Communication n where n.subject=:subject and ((n.sentTo=:sentto and n.creator=:creator) or (n.sentTo:creator and n.creator=:sentto))

Communication的别名丢失,您尝试通过其别名'n'(未定义)

来访问它

由于

答案 1 :(得分:1)

Hibernate正在垮台:

(n.sentTo:creator and n.creator=:sentto)
---------^

我怀疑这应该是

(n.sentTo = :creator and n.creator=:sentto)
---------^^^

答案 2 :(得分:0)

我不知道是否有错误,但您的别名n似乎没有在您的查询中定义。

你能给我们堆栈跟踪吗?