Criteria Query results in a NoResultException instead of returning object

#1

When I run the following code 

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery criteria = builder.createQuery(identityClass);
    Root root = criteria.from(identityClass);
    criteria.select(root);

    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(builder.equal(
        root.get(identityNameProp.getName()), obj.getName())); 

    criteria.where(predicates.toArray(new Predicate[predicates.size()]));

    return em.createQuery(criteria).getSingleResult();

which evaluates to the query:

SELECT $1 FROM IdentityObject $1 WHERE $1.name='demo'

 

ObjectDB throws the exception:

java.lang.RuntimeException: com.objectdb.o._NoResultException: No matching results for a unique query
at org.jboss.seam.security.IdentityImpl.authenticate(IdentityImpl.java:304)
at org.jboss.seam.security.IdentityImpl.login(IdentityImpl.java:223)
at org.jboss.seam.security.org$jboss$weld$bean-WEB-INF$lib$seam-security-impl-ManagedBean-class_org$jboss$seam$security$IdentityImpl_$$_WeldClientProxy.login(org$jboss$weld$bean-WEB-INF$lib$seam-security-impl-ManagedBean-class_org$jboss$seam$security$IdentityImpl_$$_WeldClientProxy.java)
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 com.sun.el.parser.AstValue.invoke(AstValue.java:234)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)
Caused by: com.objectdb.o._NoResultException: No matching results for a unique query
at com.objectdb.o.JPE.g(JPE.java:100)
at com.objectdb.o.ERR.f(ERR.java:59)
at com.objectdb.o.OBC.onObjectDBError(OBC.java:1444)
at com.objectdb.jpa.JpaQuery.getSingleResult(JpaQuery.java:620)
at org.jboss.seam.security.management.picketlink.JpaIdentityStore.lookupIdentity(JpaIdentityStore.java:999)
at org.jboss.seam.security.management.picketlink.JpaIdentityStore.validateCredential(JpaIdentityStore.java:1587)
at org.picketlink.idm.impl.repository.WrapperIdentityStoreRepository.validateCredential(WrapperIdentityStoreRepository.java:303)
at org.picketlink.idm.impl.api.session.managers.AttributesManagerImpl.validateCredentials(AttributesManagerImpl.java:607)
at com.asalsolutions.seamsecurity.auth.SimpleAuthenticator.authenticate(SimpleAuthenticator.java:86)
at org.jboss.seam.security.IdentityImpl.authenticate(IdentityImpl.java:284)
... 45 more

 

Even though there exists an object that matches the query (check attached screen shot).

Should the query be returning an object or am I doing something wrong?

 

Thanks.

 

ObjectDB version: 2.2.4_03

#2

Thew following line is unclear:

predicates.add(builder.equal(root.get(identityNameProp.getName()), obj.getName()));

To build a query that is equivalent to the following JPQL query:

SELECT $1 FROM IdentityObject $1 WHERE $1.name='demo'

You have to use root.get("name").

Maybe identityNameProp.getName() returns "name" but this cannot be determined by the code you posted. In this is the case - could you please post a complete example that can be run?

 

ObjectDB Support
#3

identityNameProp.getName() does return "name." 

Also, the JPQL equivalent of the criteria query returns an object:

        String queryStr = "select i from " + identityClass.getSimpleName() +
             " i where i." + identityNameProp.getName() +
             " = :identityObjectName";
        Object val = em.createQuery(queryStr)
                .setParameter("identityObjectName", obj.getName())
                .getSingleResult();

queryStr evaluates to:

select i from IdentityObject i where i.name = :identityObjectName

 

My issue is that the CriteriaQuery version does not return anything while the JPQL version returns the object.

 

Here are more runnable versions of the CriteriaQuery and JPQL snippets:

        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery criteria = builder.createQuery(identityClass);
        Root root = criteria.from(identityClass);
        criteria.select(root);

        List<Predicate> predicates = new ArrayList<Predicate>();
        predicates.add(builder.equal(root.get("name"), "demo"));

        criteria.where(predicates.toArray(new Predicate[predicates.size()]));

        return em.createQuery(criteria).getSingleResult();

 

        String queryStr = "select i from " + identityClass.getSimpleName() +
             " i where i.name = :identityObjectName";
        Object val = em.createQuery(queryStr)
                .setParameter("identityObjectName", "demo")
                .getSingleResult();

 

#4

OK. Your program demonstrates a bug in the new mechanism of ObjectDB 2.2.4 that converts literals to implicit parameters (to improve performance and to make using entity objects in queries easier).

Please try build 2.4.4_04 that should fix this problem. Thank you for your report.

 

ObjectDB Support
#5

Criteria query now returns the object! Thank you for resolving it so quickly.

Reply