Search in sources :

Example 71 with NoResultException

use of javax.persistence.NoResultException in project spring-framework by spring-projects.

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersons.

@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersons() {
    EntityManager em = entityManagerFactory.createEntityManager();
    Query q = em.createQuery("select p from Person as p");
    List<Person> people = q.getResultList();
    assertEquals(0, people.size());
    try {
        assertNull(q.getSingleResult());
        fail("Should have thrown NoResultException");
    } catch (NoResultException ex) {
    // expected
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 72 with NoResultException

use of javax.persistence.NoResultException in project spring-framework by spring-projects.

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersonsShared.

@Test
@SuppressWarnings({ "unused", "unchecked" })
public void testQueryNoPersonsShared() {
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
    Query q = em.createQuery("select p from Person as p");
    q.setFlushMode(FlushModeType.AUTO);
    List<Person> people = q.getResultList();
    try {
        assertNull(q.getSingleResult());
        fail("Should have thrown NoResultException");
    } catch (NoResultException ex) {
    // expected
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 73 with NoResultException

use of javax.persistence.NoResultException in project spring-framework by spring-projects.

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersonsSharedNotTransactional.

@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsSharedNotTransactional() {
    endTransaction();
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
    Query q = em.createQuery("select p from Person as p");
    q.setFlushMode(FlushModeType.AUTO);
    List<Person> people = q.getResultList();
    assertEquals(0, people.size());
    try {
        assertNull(q.getSingleResult());
        fail("Should have thrown IllegalStateException");
    } catch (Exception ex) {
        // We would typically expect an IllegalStateException, but Hibernate throws a
        // PersistenceException. So we assert the contents of the exception message instead.
        assertTrue(ex.getMessage().contains("closed"));
    }
    q = em.createQuery("select p from Person as p");
    q.setFlushMode(FlushModeType.AUTO);
    try {
        assertNull(q.getSingleResult());
        fail("Should have thrown NoResultException");
    } catch (NoResultException ex) {
    // expected
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) Person(org.springframework.orm.jpa.domain.Person) EntityNotFoundException(javax.persistence.EntityNotFoundException) NoResultException(javax.persistence.NoResultException) Test(org.junit.Test)

Example 74 with NoResultException

use of javax.persistence.NoResultException in project spring-framework by spring-projects.

the class AbstractContainerEntityManagerFactoryIntegrationTests method testQueryNoPersonsNotTransactional.

@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsNotTransactional() {
    EntityManager em = entityManagerFactory.createEntityManager();
    Query q = em.createQuery("select p from Person as p");
    List<Person> people = q.getResultList();
    assertEquals(0, people.size());
    try {
        assertNull(q.getSingleResult());
        fail("Should have thrown NoResultException");
    } catch (NoResultException ex) {
    // expected
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Example 75 with NoResultException

use of javax.persistence.NoResultException in project webpieces by deanhiller.

the class UserTestDbo method findWithJoin.

public static UserTestDbo findWithJoin(EntityManager mgr, int id) {
    Query query = mgr.createNamedQuery("findByIdWithRoleJoin");
    query.setParameter("id", id);
    try {
        return (UserTestDbo) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}
Also used : Query(javax.persistence.Query) NamedQuery(javax.persistence.NamedQuery) NoResultException(javax.persistence.NoResultException)

Aggregations

NoResultException (javax.persistence.NoResultException)169 Query (javax.persistence.Query)130 EntityManager (javax.persistence.EntityManager)40 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)17 NonUniqueResultException (javax.persistence.NonUniqueResultException)15 Test (org.junit.Test)14 Transactional (org.springframework.transaction.annotation.Transactional)14 UnitOfWork (com.google.inject.persist.UnitOfWork)12 ConfigurationStoreException (org.nhindirect.config.store.ConfigurationStoreException)9 TblMle (com.intel.mtwilson.as.data.TblMle)8 IOException (java.io.IOException)8 TblModuleManifest (com.intel.mtwilson.as.data.TblModuleManifest)7 WebResource (org.asqatasun.entity.subject.WebResource)6 ASException (com.intel.mountwilson.as.common.ASException)5 IllegalOrphanException (com.intel.mtwilson.as.controller.exceptions.IllegalOrphanException)5 NonexistentEntityException (com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException)5 UserAccount (com.jappstart.model.auth.UserAccount)5 List (java.util.List)5 Parameter (org.asqatasun.entity.parameterization.Parameter)5 Transactional (com.google.inject.persist.Transactional)4