Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » javax.persistence.criteria.Predicate cannot be passed across methods(criteria API find by Predicate )
javax.persistence.criteria.Predicate cannot be passed across methods [message #1721789] Sat, 30 January 2016 14:54 Go to next message
yunjie zh is currently offline yunjie zhFriend
Messages: 7
Registered: October 2015
Junior Member
The following code snippet will recreate the problem, the Predicate cannot be passed to findByType2. If we call entityManager.createQuery(criteriaQuery.select(root).where(p)).getSingleResult();
in the findByType() directly, it would work fine.
Any ideas?

public MyObject findByType(final MyObject.Type type)
{
    final EntityManager entityManager = getEntityManager();
    final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    final CriteriaQuery<MyObject> criteriaQuery = builder
        .createQuery(MyObject.class);
    final Root<MyObject> root = criteriaQuery.from(MyObject.class);

    final Predicate p = builder.equal(
        root.<MyObject.Type>get(MyObject_.type), type);

    return findByType2(builder, criteriaQuery, p);
  }
  
  public MyObject findByType2(final CriteriaBuilder builder,
      final CriteriaQuery<MyObject> criteriaQuery,
      final Predicate p)
  {
    final EntityManager entityManager = getEntityManager();
    final Root<MyObject> root = criteriaQuery.from(MyObject.class);

    return entityManager.createQuery(
        criteriaQuery.select(root).where(
            p)).getSingleResult();
  }
Re: javax.persistence.criteria.Predicate cannot be passed across methods [message #1721936 is a reply to message #1721789] Mon, 01 February 2016 17:43 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1380
Registered: July 2009
Senior Member
Maybe explain what is going wrong when you execute this code, and show the code that works. You are creating two EntityManagers and two Roots, so I wouldn't expect the above code to work as you are selecting from a different root then you used to build your predicate.
Re: javax.persistence.criteria.Predicate cannot be passed across methods [message #1722701 is a reply to message #1721936] Mon, 08 February 2016 11:55 Go to previous message
yunjie zh is currently offline yunjie zhFriend
Messages: 7
Registered: October 2015
Junior Member
Thanks for your reply. I think I made a mistake to describe the problem. I created a new topic at https://www.eclipse.org/forums/index.php/m/1722700/#msg_1722700.
Could you please have a look and coach me the problem? thanks.
Previous Topic:Where is the Javadoc for JPA when using EclipseLink ?
Next Topic:A javax.persistence.NonUniqueResultException Problem with Criteria API
Goto Forum:
  


Current Time: Sat Mar 06 05:50:17 GMT 2021

Powered by FUDForum. Page generated in 0.02385 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top