在jpql中使用多个AND

时间:2013-03-27 18:25:51

标签: java jpql

我使用以下jpql进行查询,但它给了我一个错误,我不知道如何在一个语句中使用多个AND。我添加了一个例外,我正在质疑。

1)查询

Query query = em.createQuery(
                "SELECT n FROM Notification n WHERE (n.recipientEmail='"
                        + user.getEmail()
                        + "' AND n.isUnread='"+1+"') AND n.growToshow='" + 1
                        + "' ORDER BY n.createdTime desc", Notification.class);

2)例外

SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/FreeBird_New] threw exception [An exception occurred while creating a query in EntityManager: 
Exception Description: Error compiling the query [SELECT n FROM Notification n WHERE (n.recipientEmail='amlan@ymail.com' AND n.isUnread='1') AND n.growToshow='1' ORDER BY n.createdTime desc], line 1, column 97: unknown state or association field [growToshow] of class [com.entity.Notification].] with root cause
Local Exception Stack: 
Exception [EclipseLink-8030] (Eclipse Persistence Services - 2.3.3.v20120629-r11760): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [SELECT n FROM Notification n WHERE (n.recipientEmail='amlan@ymail.com' AND n.isUnread='1') AND n.growToshow='1' ORDER BY n.createdTime desc], line 1, column 97: unknown state or association field [growToshow] of class [com.entity.Notification].
    at org.eclipse.persistence.exceptions.JPQLException.unknownAttribute(JPQLException.java:457)
    at org.eclipse.persistence.internal.jpa.parsing.DotNode.validate(DotNode.java:87)
    at org.eclipse.persistence.internal.jpa.parsing.Node.validate(Node.java:92)
    at org.eclipse.persistence.internal.jpa.parsing.BinaryOperatorNode.validate(BinaryOperatorNode.java:34)
    at org.eclipse.persistence.internal.jpa.parsing.EqualsNode.validate(EqualsNode.java:41)
    at org.eclipse.persistence.internal.jpa.parsing.Node.validate(Node.java:95)
    at org.eclipse.persistence.internal.jpa.parsing.LogicalOperatorNode.validate(LogicalOperatorNode.java:39)
    at org.eclipse.persistence.internal.jpa.parsing.WhereNode.validate(WhereNode.java:34)
    at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:207)
    at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:183)
    at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:173)
    at org.eclipse.persistence.internal.jpa.parsing.JPQLParseTree.populateReadQueryInternal(JPQLParseTree.java:110)
    at org.eclipse.persistence.internal.jpa.parsing.JPQLParseTree.populateQuery(JPQLParseTree.java:84)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:219)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:190)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:126)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1475)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1497)
    at com.bean.NotificationBean.getGrowlNotificationList(NotificationBean.java:73)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:71)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:106)
    at com.sun.faces.facelets.tag.jstl.core.ForEachHandler.apply(ForEachHandler.java:158)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:131)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:162)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:114)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:94)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:89)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:94)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:79)
    at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:148)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:734)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:96)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:309)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:1)

您的问题似乎是growToshow课程中没有Notification属性,但我还有其他建议:

使用连接来设置参数。

JPA为您提供了一种更安全的方法来定义参数,我将以您的查询为例:

StringBuilder jpql = new StringBuilder()
jpql.append(" SELECT n FROM Notification n ");
jpql.append("   WHERE n.recipientEmail = :recipientEmail ");
jpql.append("   AND n.isUnread = :isUnread ");
jpql.append("   AND n.growToshow = :growToshow ");
jpql.append(" ORDER BY n.createdTime DESC ");

TypedQuery<Notification> query = em.createQuery(jpql.toString(), Notification.class);

query.setParameter("recipientEmail", user.getEmail());
query.setParameter("isUnread", 1);
query.setParameter("growToshow", 1);

请参阅?只需使用命名参数,您的查询看起来更具可读性,并且可以避免SQL注入等问题。