Search in sources :

Example 41 with EntityNotFoundException

use of javax.persistence.EntityNotFoundException in project hibernate-orm by hibernate.

the class JPAProxyTest method testGetSemantics.

/**
	 * The ejb3 find() method maps to the Hibernate get() method
	 */
@Test
public void testGetSemantics() {
    Long nonExistentId = new Long(-1);
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    Item item = (Item) s.get(Item.class, nonExistentId);
    assertNull("get() of non-existent entity did not return null", item);
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    // first load() it to generate a proxy...
    item = (Item) s.load(Item.class, nonExistentId);
    assertFalse(Hibernate.isInitialized(item));
    // then try to get() it to make sure we get an exception
    try {
        s.get(Item.class, nonExistentId);
        fail("force load did not fail on non-existent entity");
    } catch (EntityNotFoundException e) {
    // expected behavior
    } catch (AssertionFailedError e) {
        throw e;
    } catch (Throwable t) {
        fail("unexpected exception type on non-existent entity force load : " + t);
    }
    txn.commit();
    s.close();
}
Also used : Item(org.hibernate.test.jpa.Item) Transaction(org.hibernate.Transaction) EntityNotFoundException(javax.persistence.EntityNotFoundException) AssertionFailedError(junit.framework.AssertionFailedError) Session(org.hibernate.Session) AbstractJPATest(org.hibernate.test.jpa.AbstractJPATest) Test(org.junit.Test)

Example 42 with EntityNotFoundException

use of javax.persistence.EntityNotFoundException in project hibernate-orm by hibernate.

the class JPAProxyTest method testEjb3ProxyUsage.

@Test
public void testEjb3ProxyUsage() {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    Item item = (Item) s.load(Item.class, new Long(-1));
    assertFalse(Hibernate.isInitialized(item));
    try {
        Hibernate.initialize(item);
        fail("proxy access did not fail on non-existent proxy");
    } catch (EntityNotFoundException e) {
    // expected behavior
    } catch (Throwable t) {
        fail("unexpected exception type on non-existent proxy access : " + t);
    }
    s.clear();
    Item item2 = (Item) s.load(Item.class, new Long(-1));
    assertFalse(Hibernate.isInitialized(item2));
    assertFalse(item == item2);
    try {
        item2.getName();
        fail("proxy access did not fail on non-existent proxy");
    } catch (EntityNotFoundException e) {
    // expected behavior
    } catch (Throwable t) {
        fail("unexpected exception type on non-existent proxy access : " + t);
    }
    txn.commit();
    s.close();
}
Also used : Item(org.hibernate.test.jpa.Item) Transaction(org.hibernate.Transaction) EntityNotFoundException(javax.persistence.EntityNotFoundException) Session(org.hibernate.Session) AbstractJPATest(org.hibernate.test.jpa.AbstractJPATest) Test(org.junit.Test)

Example 43 with EntityNotFoundException

use of javax.persistence.EntityNotFoundException in project hibernate-orm by hibernate.

the class EntityManagerTest method testEntityNotFoundException.

@Test
public void testEntityNotFoundException() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Wallet w = new Wallet();
    w.setBrand("Lacoste");
    w.setModel("Minimic");
    w.setSerial("0324");
    em.persist(w);
    Wallet wallet = em.find(Wallet.class, w.getSerial());
    em.createNativeQuery("delete from Wallet").executeUpdate();
    try {
        em.refresh(wallet);
    } catch (EntityNotFoundException enfe) {
        // success
        if (em.getTransaction() != null) {
            em.getTransaction().rollback();
        }
        em.close();
        return;
    }
    try {
        em.getTransaction().commit();
        fail("Should have raised an EntityNotFoundException");
    } catch (PersistenceException pe) {
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) EntityNotFoundException(javax.persistence.EntityNotFoundException) Test(org.junit.Test)

Example 44 with EntityNotFoundException

use of javax.persistence.EntityNotFoundException in project hibernate-orm by hibernate.

the class EntityManagerTest method testGet.

@Test
@TestForIssue(jiraKey = "EJB-9")
public void testGet() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Item item = em.getReference(Item.class, "nonexistentone");
    try {
        item.getDescr();
        em.getTransaction().commit();
        fail("Object with wrong id should have failed");
    } catch (EntityNotFoundException e) {
        //success
        if (em.getTransaction() != null) {
            em.getTransaction().rollback();
        }
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) EntityNotFoundException(javax.persistence.EntityNotFoundException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 45 with EntityNotFoundException

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

the class AbstractContainerEntityManagerFactoryIntegrationTests method testGetReferenceWhenNoRow.

@Test
public void testGetReferenceWhenNoRow() {
    try {
        Person notThere = sharedEntityManager.getReference(Person.class, 666);
        // We may get here (as with Hibernate).
        // Either behaviour is valid: throw exception on first access
        // or on getReference itself.
        notThere.getFirstName();
        fail("Should have thrown an EntityNotFoundException");
    } catch (EntityNotFoundException ex) {
    // expected
    }
}
Also used : EntityNotFoundException(javax.persistence.EntityNotFoundException) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.Test)

Aggregations

EntityNotFoundException (javax.persistence.EntityNotFoundException)75 EntityManager (javax.persistence.EntityManager)20 NonexistentEntityException (com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException)18 ServiceException (gov.ca.cwds.rest.services.ServiceException)15 Transactional (org.springframework.transaction.annotation.Transactional)14 BusinessServiceException (sic.service.BusinessServiceException)11 Test (org.junit.Test)10 Calendar (java.util.Calendar)9 GregorianCalendar (java.util.GregorianCalendar)9 ArrayList (java.util.ArrayList)7 BooleanBuilder (com.querydsl.core.BooleanBuilder)6 TblMle (com.intel.mtwilson.as.data.TblMle)5 IllegalOrphanException (com.intel.mtwilson.as.controller.exceptions.IllegalOrphanException)4 TblModuleManifest (com.intel.mtwilson.as.data.TblModuleManifest)4 PersistenceException (javax.persistence.PersistenceException)4 Sort (org.springframework.data.domain.Sort)4 TblHosts (com.intel.mtwilson.as.data.TblHosts)3 OptimisticLockException (javax.persistence.OptimisticLockException)3 TblEventType (com.intel.mtwilson.as.data.TblEventType)2 TblHostSpecificManifest (com.intel.mtwilson.as.data.TblHostSpecificManifest)2