Search in sources :

Example 1 with ObjectNotFoundException

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

the class CollectionCacheEvictionTest method testCollectionCacheEvictionRemoveWithEntityOutOfContext.

@Test
public void testCollectionCacheEvictionRemoveWithEntityOutOfContext() {
    Session s = openSession();
    Company company = s.get(Company.class, 1);
    assertEquals(1, company.getUsers().size());
    s.close();
    s = openSession();
    s.beginTransaction();
    s.delete(company.getUsers().get(0));
    s.getTransaction().commit();
    s.close();
    s = openSession();
    company = s.get(Company.class, 1);
    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)

Example 2 with ObjectNotFoundException

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

the class CollectionCacheEvictionTest method testCollectionCacheEvictionUpdateWithEntityOutOfContext.

@Test
public void testCollectionCacheEvictionUpdateWithEntityOutOfContext() {
    Session s = openSession();
    Company company1 = s.get(Company.class, 1);
    Company company2 = s.get(Company.class, 2);
    assertEquals(1, company1.getUsers().size());
    assertEquals(0, company2.getUsers().size());
    s.close();
    s = openSession();
    s.beginTransaction();
    User user = s.get(User.class, 1);
    user.setCompany(company2);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    company1 = s.get(Company.class, 1);
    company2 = s.get(Company.class, 2);
    assertEquals(1, company2.getUsers().size());
    try {
        assertEquals(0, company1.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)

Example 3 with ObjectNotFoundException

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

the class CollectionCacheEvictionTest method testCollectionCacheEvictionUpdate.

@Test
public void testCollectionCacheEvictionUpdate() {
    Session s = openSession();
    s.beginTransaction();
    Company company1 = (Company) s.get(Company.class, 1);
    Company company2 = (Company) s.get(Company.class, 2);
    // init cache of collection
    assertEquals(1, company1.getUsers().size());
    assertEquals(0, company2.getUsers().size());
    User user = (User) s.get(User.class, 1);
    user.setCompany(company2);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    company1 = (Company) s.get(Company.class, 1);
    company2 = (Company) s.get(Company.class, 2);
    assertEquals(1, company2.getUsers().size());
    // fails if cache is not evicted
    try {
        assertEquals(0, company1.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)

Example 4 with ObjectNotFoundException

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

the class ParentChildTest method testLoadAfterNonExists.

@Test
public void testLoadAfterNonExists() throws HibernateException, SQLException {
    Session session = openSession();
    if ((getDialect() instanceof MySQLDialect) || (getDialect() instanceof IngresDialect)) {
        session.doWork(new AbstractWork() {

            @Override
            public void execute(Connection connection) throws SQLException {
                connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            }
        });
    }
    session.getTransaction().begin();
    // First, prime the fixture session to think the entity does not exist
    try {
        session.load(Simple.class, new Long(-1));
        fail();
    } catch (ObjectNotFoundException onfe) {
        if (getDialect() instanceof TeradataDialect) {
            session.getTransaction().rollback();
            session.getTransaction().begin();
        }
    // this is correct
    }
    // Next, lets create that entity "under the covers"
    Session anotherSession = sessionFactory().openSession();
    anotherSession.beginTransaction();
    Simple myNewSimple = new Simple(Long.valueOf(-1));
    myNewSimple.setName("My under the radar Simple entity");
    myNewSimple.setAddress("SessionCacheTest.testLoadAfterNonExists");
    myNewSimple.setCount(1);
    myNewSimple.setDate(new Date());
    myNewSimple.setPay(Float.valueOf(100000000));
    anotherSession.save(myNewSimple);
    anotherSession.getTransaction().commit();
    anotherSession.close();
    // Now, lets make sure the original session can see the created row...
    session.clear();
    try {
        Simple dummy = (Simple) session.get(Simple.class, Long.valueOf(-1));
        assertNotNull("Unable to locate entity Simple with id = -1", dummy);
        session.delete(dummy);
    } catch (ObjectNotFoundException onfe) {
        fail("Unable to locate entity Simple with id = -1");
    }
    session.getTransaction().commit();
    session.close();
}
Also used : MySQLDialect(org.hibernate.dialect.MySQLDialect) SQLException(java.sql.SQLException) AbstractWork(org.hibernate.jdbc.AbstractWork) IngresDialect(org.hibernate.dialect.IngresDialect) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) Connection(java.sql.Connection) TeradataDialect(org.hibernate.dialect.TeradataDialect) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with ObjectNotFoundException

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

the class InsSession method getXMLEvent.

private Event getXMLEvent(final OnmsEvent ev) {
    final Integer id = ev.getId();
    LOG.info("Working on XML Event for id: {}", id);
    LOG.debug("Setting Event id: {}", id);
    final Event e = new Event();
    e.setDbid(id);
    //UEI
    final String uei = ev.getEventUei();
    if (uei != null) {
        LOG.debug("Setting Event uei: {}", uei);
        e.setUei(uei);
    } else {
        LOG.warn("No Event uei found: skipping event....");
        return null;
    }
    // Source
    final String source = ev.getEventSource();
    if (source != null) {
        LOG.debug("Setting Event source: {}", source);
        e.setSource(source);
    } else {
        LOG.info("No Event source found.");
    }
    //nodeid
    final Integer nodeid = ev.getNode().getId();
    if (ev.getNode() != null && nodeid != null) {
        LOG.debug("Setting Event nodeid: {}", nodeid);
        e.setNodeid(nodeid.longValue());
    } else {
        LOG.info("No Event node found.");
    }
    // timestamp
    final Date time = ev.getEventTime();
    if (time != null) {
        LOG.debug("Setting event date timestamp to (GMT): {}", time);
        e.setTime(time);
    } else {
        LOG.info("No Event time found.");
    }
    // host
    final String host = ev.getEventHost();
    if (host != null) {
        LOG.debug("Setting Event Host: {}", host);
        e.setHost(host);
    } else {
        LOG.info("No Event host found.");
    }
    // interface
    final InetAddress ipAddr = ev.getIpAddr();
    if (ipAddr != null) {
        LOG.debug("Setting Event Interface/ipaddress: {}", ipAddr);
        e.setInterfaceAddress(ipAddr);
    } else {
        LOG.info("No Event ip address found.");
    }
    // Service Name
    if (ev.getServiceType() != null) {
        final String serviceName = ev.getServiceType().getName();
        LOG.debug("Setting Event Service Name: {}", serviceName);
        e.setService(serviceName);
    } else {
        LOG.info("No Event service name found.");
    }
    // Description
    final String descr = ev.getEventDescr();
    if (descr != null) {
        LOG.debug("Setting Event Description: {}", descr);
        e.setDescr(descr);
    } else {
        LOG.info("No Event ip address found.");
    }
    // Log message
    final String logmsg = ev.getEventLogMsg();
    if (logmsg != null) {
        final Logmsg msg = new Logmsg();
        LOG.debug("Setting Event Log Message: {}", logmsg);
        msg.setContent(logmsg);
        e.setLogmsg(msg);
    } else {
        LOG.info("No Event log Message found.");
    }
    // severity
    final Integer severity = ev.getEventSeverity();
    if (severity != null) {
        LOG.debug("Setting Event Severity: {}", severity);
        e.setSeverity(OnmsSeverity.get(severity).getLabel());
    } else {
        LOG.info("No Event severity found.");
    }
    final Integer ifIndex = ev.getIfIndex();
    if (ifIndex != null && ifIndex > 0) {
        e.setIfIndex(ifIndex);
        e.setIfAlias(getIfAlias(nodeid, ifIndex));
    } else {
        e.setIfIndex(-1);
        e.setIfAlias("-1");
    }
    // operator Instruction
    final String operInstruct = ev.getEventOperInstruct();
    if (operInstruct != null) {
        LOG.debug("Setting Event Operator Instruction: {}", operInstruct);
        e.setOperinstruct(operInstruct);
    } else {
        LOG.info("No Event operator Instruction found.");
    }
    // parms
    final String eventParms = ev.getEventParms();
    if (eventParms != null) {
        LOG.debug("Setting Event Parms: {}", eventParms);
        final List<Parm> parms = EventParameterUtils.decode(eventParms);
        if (parms != null)
            e.setParmCollection(parms);
    } else {
        LOG.info("No Event parms found.");
    }
    final AlarmData ad = new AlarmData();
    final OnmsAlarm onmsAlarm = ev.getAlarm();
    try {
        if (onmsAlarm != null) {
            ad.setReductionKey(onmsAlarm.getReductionKey());
            ad.setAlarmType(onmsAlarm.getAlarmType());
            ad.setClearKey(onmsAlarm.getClearKey());
            e.setAlarmData(ad);
        }
    } catch (final ObjectNotFoundException e1) {
        LOG.warn("Correlated alarm data not found.", e1);
    }
    LOG.info("Returning event with id: {}", id);
    return e;
}
Also used : Logmsg(org.opennms.netmgt.xml.event.Logmsg) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Event(org.opennms.netmgt.xml.event.Event) Parm(org.opennms.netmgt.xml.event.Parm) InetAddress(java.net.InetAddress) Date(java.util.Date) AlarmData(org.opennms.netmgt.xml.event.AlarmData)

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