Search in sources :

Example 1 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project Asqatasun by Asqatasun.

the class OptionElementDAOImpl method findOptionElementFromUser.

@Override
public Collection<OptionElement> findOptionElementFromUser(User user) {
    Query query = entityManager.createQuery("SELECT u.optionElementSet FROM " + getUserEntityClass().getName() + " u" + " WHERE u=:user");
    query.setParameter("user", user);
    try {
        return (Collection<OptionElement>) query.getResultList();
    } catch (NoResultException | NonUniqueResultException nre) {
        return null;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) Query(javax.persistence.Query) Collection(java.util.Collection) NoResultException(javax.persistence.NoResultException)

Example 2 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project Asqatasun by Asqatasun.

the class OptionElementDAOImpl method findOptionElementFromValueAndOption.

/**
     * 
     * @param value
     * @param option
     * @return 
     */
@Override
public OptionElement findOptionElementFromValueAndOption(String value, Option option) {
    Query query = entityManager.createQuery("SELECT oe FROM " + getEntityClass().getName() + " oe" + " WHERE oe.value=:value" + " AND oe.option=:option");
    query.setParameter("value", value);
    query.setParameter("option", option);
    try {
        return (OptionElement) query.getSingleResult();
    } catch (NoResultException | NonUniqueResultException nre) {
        return null;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) Query(javax.persistence.Query) OptionElement(org.asqatasun.webapp.entity.option.OptionElement) NoResultException(javax.persistence.NoResultException)

Example 3 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project invesdwin-context-persistence by subes.

the class SimpleTestService method getByName.

public SimpleTestEntity getByName(final String name) {
    final SimpleTestEntity example = new SimpleTestEntity();
    example.setName(name);
    final List<SimpleTestEntity> l = dao.findAll(example);
    if (l.size() == 0) {
        return null;
    } else if (l.size() > 1) {
        throw new NonUniqueResultException(String.valueOf(l.size()));
    } else {
        return l.get(0);
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException)

Example 4 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project invesdwin-context-persistence by subes.

the class SimpleTestService method getByName.

public SimpleTestEntity getByName(final String name) {
    final SimpleTestEntity example = new SimpleTestEntity();
    example.setName(name);
    final List<SimpleTestEntity> l = dao.findAll(example);
    if (l.size() == 0) {
        return null;
    } else if (l.size() > 1) {
        throw new NonUniqueResultException(String.valueOf(l.size()));
    } else {
        return l.get(0);
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException)

Example 5 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project tests by datanucleus.

the class JPQLQueryTest method testNonUniqueResultExceptionThrown.

public void testNonUniqueResultExceptionThrown() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            em.persist(p1);
            Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@jpox.com");
            em.persist(p2);
            em.createQuery("SELECT Object(T) FROM " + Person.class.getName() + " T where T.firstName <> 'Wilma' ").getSingleResult();
            fail("expected NonUniqueResultException");
        } catch (NonUniqueResultException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Person(org.datanucleus.samples.annotations.models.company.Person)

Aggregations

NonUniqueResultException (javax.persistence.NonUniqueResultException)39 NoResultException (javax.persistence.NoResultException)33 Query (javax.persistence.Query)23 EntityManager (javax.persistence.EntityManager)5 Collection (java.util.Collection)4 List (java.util.List)4 PersistenceException (javax.persistence.PersistenceException)4 WebResource (org.asqatasun.entity.subject.WebResource)3 HibernateException (org.hibernate.HibernateException)3 RoleAssignment (edu.harvard.iq.dataverse.RoleAssignment)2 BuiltinUser (edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUser)2 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)2 PasswordResetData (edu.harvard.iq.dataverse.passwordreset.PasswordResetData)2 IOException (java.io.IOException)2 Date (java.util.Date)2 EntityExistsException (javax.persistence.EntityExistsException)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)2 LockTimeoutException (javax.persistence.LockTimeoutException)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 PessimisticLockException (javax.persistence.PessimisticLockException)2