Search in sources :

Example 6 with ObjectNotFoundException

use of org.hibernate.ObjectNotFoundException in project opennms by OpenNMS.

the class AcknowledgmentDaoHibernate method findAcknowledgables.

/** {@inheritDoc} */
@Override
public List<Acknowledgeable> findAcknowledgables(final OnmsAcknowledgment ack) {
    List<Acknowledgeable> ackables = new ArrayList<Acknowledgeable>();
    if (ack == null || ack.getAckType() == null) {
        return ackables;
    }
    if (ack.getAckType().equals(AckType.ALARM)) {
        final OnmsAlarm alarm = findAlarm(ack);
        try {
            if (alarm != null && alarm.getAckId() != null) {
                ackables.add(alarm);
                List<OnmsNotification> notifs = findRelatedNotifications(alarm);
                if (notifs != null) {
                    for (OnmsNotification notif : notifs) {
                        try {
                            if (notif.getAckId() != null) {
                                ackables.add(notif);
                            }
                        } catch (final ObjectNotFoundException e) {
                            LOG.warn("found ackables for alarm #{} but ackable was invalid", ack.getRefId(), e);
                        }
                    }
                }
            }
        } catch (final ObjectNotFoundException e) {
            LOG.warn("unable to find alarm with ID {}", ack.getRefId(), e);
        }
    } else if (ack.getAckType().equals(AckType.NOTIFICATION)) {
        final OnmsNotification notif = findNotification(ack);
        try {
            if (notif != null && notif.getAckId() != null) {
                ackables.add(notif);
                try {
                    if (notif.getEvent() != null) {
                        final OnmsAlarm alarm = notif.getEvent().getAlarm();
                        if (alarm != null) {
                            ackables.add(alarm);
                        }
                    }
                } catch (final ObjectNotFoundException e) {
                    LOG.warn("unable to find alarm for notification #{}", notif.getNotifyId(), e);
                }
            }
        } catch (final ObjectNotFoundException e) {
            LOG.warn("unable to find notification with ID {}", ack.getRefId(), e);
        }
    }
    return ackables;
}
Also used : Acknowledgeable(org.opennms.netmgt.model.Acknowledgeable) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) ArrayList(java.util.ArrayList) OnmsNotification(org.opennms.netmgt.model.OnmsNotification)

Example 7 with ObjectNotFoundException

use of org.hibernate.ObjectNotFoundException in project hibernate-orm by hibernate.

the class MasterDetailTest method testCustomPersister.

@Test
public void testCustomPersister() throws Exception {
    Session s = openSession();
    Custom c = new Custom();
    c.setName("foo");
    c.id = "100";
    s.beginTransaction();
    String id = id = (String) s.save(c);
    assertTrue(c == s.load(Custom.class, id));
    s.flush();
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    c = (Custom) s.load(Custom.class, id);
    assertTrue(c.getName().equals("foo"));
    c.setName("bar");
    s.flush();
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    c = (Custom) s.load(Custom.class, id);
    assertTrue(c.getName().equals("bar"));
    s.delete(c);
    s.flush();
    s.getTransaction().commit();
    s.close();
    s = openSession();
    boolean none = false;
    try {
        s.load(Custom.class, id);
    } catch (ObjectNotFoundException onfe) {
        none = true;
    }
    assertTrue(none);
    s.close();
}
Also used : ObjectNotFoundException(org.hibernate.ObjectNotFoundException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with ObjectNotFoundException

use of org.hibernate.ObjectNotFoundException in project hibernate-orm by hibernate.

the class FooBarTest method testLoadAfterDelete.

@Test
public void testLoadAfterDelete() throws Exception {
    Session s = openSession();
    s.beginTransaction();
    Foo foo = new Foo();
    Serializable id = s.save(foo);
    s.flush();
    s.delete(foo);
    boolean err = false;
    try {
        s.load(Foo.class, id);
    } catch (ObjectNotFoundException ode) {
        err = true;
    }
    assertTrue(err);
    s.flush();
    err = false;
    try {
        ((FooProxy) s.load(Foo.class, id)).getBool();
    } catch (ObjectNotFoundException onfe) {
        err = true;
    }
    assertTrue(err);
    //yuck!!
    id = FumTest.fumKey("abc");
    Fo fo = Fo.newFo((FumCompositeID) id);
    s.save(fo);
    s.flush();
    s.delete(fo);
    err = false;
    try {
        s.load(Fo.class, id);
    } catch (ObjectNotFoundException ode) {
        err = true;
    }
    assertTrue(err);
    s.flush();
    err = false;
    try {
        s.load(Fo.class, id);
    } catch (ObjectNotFoundException onfe) {
        err = true;
    }
    assertTrue(err);
    s.getTransaction().commit();
    s.close();
}
Also used : Serializable(java.io.Serializable) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 9 with ObjectNotFoundException

use of org.hibernate.ObjectNotFoundException in project hibernate-orm by hibernate.

the class ProxyTest method testSubsequentNonExistentProxyAccess.

@Test
public void testSubsequentNonExistentProxyAccess() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    DataPoint proxy = (DataPoint) s.load(DataPoint.class, new Long(-1));
    assertFalse(Hibernate.isInitialized(proxy));
    try {
        proxy.getDescription();
        fail("proxy access did not fail on non-existent proxy");
    } catch (ObjectNotFoundException onfe) {
    // expected
    } catch (Throwable e) {
        fail("unexpected exception type on non-existent proxy access : " + e);
    }
    // try it a second (subsequent) time...
    try {
        proxy.getDescription();
        fail("proxy access did not fail on non-existent proxy");
    } catch (ObjectNotFoundException onfe) {
    // expected
    } catch (Throwable e) {
        fail("unexpected exception type on non-existent proxy access : " + e);
    }
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 10 with ObjectNotFoundException

use of org.hibernate.ObjectNotFoundException in project hibernate-orm by hibernate.

the class CollectionCacheEvictionTest method testCollectionCacheEvictionRemove.

@Test
public void testCollectionCacheEvictionRemove() {
    Session s = openSession();
    s.beginTransaction();
    Company company = (Company) s.get(Company.class, 1);
    // init cache of collection
    assertEquals(1, company.getUsers().size());
    s.delete(company.getUsers().get(0));
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    company = (Company) s.get(Company.class, 1);
    // fails if cache is not evicted
    try {
        assertEquals(0, company.getUsers().size());
    } catch (ObjectNotFoundException e) {
        fail("Cached element not found");
    }
    s.close();
}
Also used : ObjectNotFoundException(org.hibernate.ObjectNotFoundException) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

ObjectNotFoundException (org.hibernate.ObjectNotFoundException)13 Session (org.hibernate.Session)9 Test (org.junit.Test)9 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)2 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)2 Serializable (java.io.Serializable)1 InetAddress (java.net.InetAddress)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 EntityExistsException (javax.persistence.EntityExistsException)1 LockTimeoutException (javax.persistence.LockTimeoutException)1 NoResultException (javax.persistence.NoResultException)1 NonUniqueResultException (javax.persistence.NonUniqueResultException)1 OptimisticLockException (javax.persistence.OptimisticLockException)1 PersistenceException (javax.persistence.PersistenceException)1 PessimisticLockException (javax.persistence.PessimisticLockException)1 QueryTimeoutException (javax.persistence.QueryTimeoutException)1 RollbackException (javax.persistence.RollbackException)1