JPA从多个表中选择错误

时间:2015-12-12 08:54:56

标签: java jpa

我是JPA的新手,我尝试从三个表中选择我得到错误。我尝试仅选择具有所选类别的产品,但产品在产品子类别下,子类别在类别表下。

产品外键到产品子类别 产品子类别产品类别的外键

如何解决这个问题?感谢。

Severe:   Local Exception Stack:   
Exception [EclipseLink-6076] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException  
Exception Description: Object comparisons can only be used with OneToOneMappings.  Other mapping comparisons must be done through query keys or direct attribute level comparisons.   
Mapping: [org.eclipse.persistence.mappings.DirectToFieldMapping[categoryid-->PRODUCTCATEGORY.CATEGORYID]]   
Expression: [  
Query Key categoryid  
Base model.ProductCategory]  
Query: ReadAllQuery(referenceClass=Product jpql="SELECT a FROM Product a, ProductCategory b, ProductSubcategory c WHERE b.categoryid=c.categoryid AND a.subcategoryid=c.subcategoryid AND b.categoryid = ?1 AND a.productstatus = ?2")  
    at org.eclipse.persistence.exceptions.QueryException.unsupportedMappingForObjectComparison(QueryException.java:1170)  
    at org.eclipse.persistence.mappings.DatabaseMapping.buildObjectJoinExpression(DatabaseMapping.java:292)  
    at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:832)  
    at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:224)  
    at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:224)  
    at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:224)  
    at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1449)  
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQueryMechanism.java:549)  
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1720)  
    at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:813)  
    at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:744)  
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:661)  
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:901)  
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:613)  
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:194)  
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)  
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)  
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)  
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)  
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:456)  
    at model.ProductService.findByCategoryID(ProductService.java:45)  
    at controller.RetrieveProductList.processRequest(RetrieveProductList.java:45)  
    at controller.RetrieveProductList.doGet(RetrieveProductList.java:82)  
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)  
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)  
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)  
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)  
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)  
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)  
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)  
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)  
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)  
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)  
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)  
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)  
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)  
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)  
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)  
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)  
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)  
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)  
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)  
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)  
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)  
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)  
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)  
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)  
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)  
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)  
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)  
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)  
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)  
    at java.lang.Thread.run(Thread.java:745)  

这是错误日志

                getWindow().getSharedElementExitTransition().addListener(new Transition.TransitionListener() {
                    @Override
                    public void onTransitionStart(Transition transition) {

                    }

                    @Override
                    public void onTransitionEnd(Transition transition) {

                    }

                    @Override
                    public void onTransitionCancel(Transition transition) {

                    }

                    @Override
                    public void onTransitionPause(Transition transition) {

                    }

                    @Override
                    public void onTransitionResume(Transition transition) {

                    }
                });

1 个答案:

答案 0 :(得分:0)

JPA检索与第一个对象关联的所有对象,因此您不必执行

query = mgr.createQuery("SELECT a FROM Product a, ProductCategory b,
ProductSubcategory c WHERE b.categoryid=c.categoryid AND 
a.subcategoryid=c.subcategoryid AND b.categoryid = ?1 AND a.productstatus = ?2");

如果你的实体有正确的关系,你只需要做

query = mgr.createQuery("SELECT a FROM Product a WHERE 
a.b.categoryid = ?1 AND a.productstatus = ?2;