Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » A javax.persistence.NonUniqueResultException Problem with Criteria API
A javax.persistence.NonUniqueResultException Problem with Criteria API [message #1722700] Mon, 08 February 2016 11:53 Go to next message
yunjie zh is currently offline yunjie zhFriend
Messages: 7
Registered: October 2015
Junior Member
The following code snippet is an example code to reproduce my production javax.persistence.NonUniqueResultException problem.
This code worked well when we use Apache OpenJPA as the persistence provider, while we migrate to eclipselink as the provider, we got the exception.
If I pass the root object in the findByType to findByType2 as a parameter, it worked.
So what's the root cause of this problem?(two root?) Why it worked in OpenJPA?
Where I can find the specification for reference to let me better understand the problem? thanks.


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(entityManager, builder, p);
  }
  
  public MyObject findByType2(EntityManager entityManager, final CriteriaBuilder builder, final Predicate p)
  {
    final CriteriaQuery<MyObject> criteriaQuery = builder
        .createQuery(MyObject.class);
    final Root<MyObject> root = criteriaQuery.from(MyObject.class);

    return entityManager.createQuery(
        criteriaQuery.select(root).where(
            p)).getSingleResult();
  }
Re: A javax.persistence.NonUniqueResultException Problem with Criteria API [message #1722719 is a reply to message #1722700] Mon, 08 February 2016 15:09 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
The specification (2.1) is at http://download.oracle.com/otndocs/jcp/persistence-2_1-fr-eval-spec/index.html

From what you've shown, the equivalent JPQL would be something like:
"Select root2 From MyObject root, MyObject root2 where root.type = type", and I would expect it to return a cartesian product of MyObject instances unless you connect the two roots in some way.

Since multiple roots can be used, I don't know why this would behave differently in another provider - you would have to check out their bugs and documentation to see if it is some JPA extension.
Previous Topic:javax.persistence.criteria.Predicate cannot be passed across methods
Next Topic:Eclipselink Connection Pool Usage
Goto Forum:
  


Current Time: Wed Apr 24 23:50:01 GMT 2024

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

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

Back to the top