Example usage for org.hibernate Session evict

List of usage examples for org.hibernate Session evict

Introduction

In this page you can find the example usage for org.hibernate Session evict.

Prototype

void evict(Object object);

Source Link

Document

Remove this instance from the session cache.

Usage

From source file:com.hazelcast.hibernate.LocalRegionFactoryDefaultTest.java

License:Open Source License

@Test
public void testEntity() {
    final HazelcastInstance hz = getHazelcastInstance(sf);
    assertNotNull(hz);/*from  www . j  a va  2  s  .  c om*/
    final int count = 100;
    final int childCount = 3;
    insertDummyEntities(count, childCount);
    List<DummyEntity> list = new ArrayList<DummyEntity>(count);
    Session session = sf.openSession();
    try {
        for (int i = 0; i < count; i++) {
            DummyEntity e = (DummyEntity) session.get(DummyEntity.class, (long) i);
            session.evict(e);
            list.add(e);
        }
    } finally {
        session.close();
    }
    session = sf.openSession();
    Transaction tx = session.beginTransaction();
    try {
        for (DummyEntity dummy : list) {
            dummy.setDate(new Date());
            session.update(dummy);
        }
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

    Statistics stats = sf.getStatistics();
    assertEquals((childCount + 1) * count, stats.getEntityInsertCount());
    // twice put of entity and properties (on load and update) and once put of collection
    assertEquals((childCount + 1) * count * 2 + count, stats.getSecondLevelCachePutCount());
    assertEquals(childCount * count, stats.getEntityLoadCount());
    assertEquals(count, stats.getSecondLevelCacheHitCount());
    // collection cache miss
    assertEquals(count, stats.getSecondLevelCacheMissCount());
    stats.logSummary();
}

From source file:com.hazelcast.hibernate.RegionFactoryDefaultTest.java

License:Open Source License

@Test
public void testEntity() {
    final HazelcastInstance hz = getHazelcastInstance(sf);
    assertNotNull(hz);//w  w w.  ja va  2s . co  m
    final int count = 100;
    final int childCount = 3;
    insertDummyEntities(count, childCount);
    List<DummyEntity> list = new ArrayList<DummyEntity>(count);
    Session session = sf.openSession();
    try {
        for (int i = 0; i < count; i++) {
            DummyEntity e = (DummyEntity) session.get(DummyEntity.class, (long) i);
            session.evict(e);
            list.add(e);
        }
    } finally {
        session.close();
    }
    session = sf.openSession();
    Transaction tx = session.beginTransaction();
    try {
        for (DummyEntity dummy : list) {
            dummy.setDate(new Date());
            session.update(dummy);
        }
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

    Statistics stats = sf.getStatistics();
    Map<?, ?> cache = hz.getMap(DummyEntity.class.getName());
    Map<?, ?> propCache = hz.getMap(DummyProperty.class.getName());
    Map<?, ?> propCollCache = hz.getMap(DummyEntity.class.getName() + ".properties");
    assertEquals((childCount + 1) * count, stats.getEntityInsertCount());
    // twice put of entity and properties (on load and update) and once put of collection
    // TODO: fix next assertion ->
    //        assertEquals((childCount + 1) * count * 2, stats.getSecondLevelCachePutCount());
    assertEquals(childCount * count, stats.getEntityLoadCount());
    assertEquals(count, stats.getSecondLevelCacheHitCount());
    // collection cache miss
    assertEquals(count, stats.getSecondLevelCacheMissCount());
    assertEquals(count, cache.size());
    assertEquals(count * childCount, propCache.size());
    assertEquals(count, propCollCache.size());
    sf.getCache().evictEntityRegion(DummyEntity.class);
    sf.getCache().evictEntityRegion(DummyProperty.class);
    assertEquals(0, cache.size());
    assertEquals(0, propCache.size());
    stats.logSummary();
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

public void evict(final Object entity) throws Exception {
    doHibernate(new HibernateCallback() {
        public Object execute(Session session) throws HibernateException {
            session.evict(entity);
            return null;
        }//from  w  w w.  j a va  2s  .c om
    });
}

From source file:com.lewischooman.dao.MovieShowDAO.java

License:Open Source License

@Override
public Status updateMovieShowWithBooking(MovieShowDB[] movieShowArr, Integer seatsBooked) {
    Session session;
    Integer showSeatsAvailable, showSeatsBooked;

    session = this.sessionFactory.getCurrentSession();
    if (session.contains(movieShowArr[0])) {
        session.evict(movieShowArr[0]);
    }// ww  w . j a  va  2  s  . c o m
    movieShowArr[0] = this.getMovieShowById(movieShowArr[0].getId(), LockOptions.UPGRADE);
    showSeatsAvailable = movieShowArr[0].getSeatsAvailable();
    showSeatsBooked = movieShowArr[0].getSeatsBooked();
    if (showSeatsAvailable - showSeatsBooked >= seatsBooked) {
        showSeatsBooked += seatsBooked;
        movieShowArr[0].setSeatsBooked(showSeatsBooked);
        movieShowArr[0].setSeatsBalance(showSeatsAvailable - showSeatsBooked);
        movieShowArr[0].setAmount(movieShowArr[0].getPrice() * showSeatsBooked);

        return Status.OK;
    } else {
        return Status.INSUFFICIENT_SEATS;
    }
}

From source file:com.mercatis.lighthouse3.persistence.commons.hibernate.DomainModelEntityDAOImplementation.java

License:Apache License

public void update(Entity entityToUpdate) {
    if (!alreadyPersisted(entityToUpdate))
        throw new PersistenceException("Entity not persistent", null);
    try {/*from   ww w.  ja  va  2s.  c o m*/
        Session sess = unitOfWork.getCurrentSession();
        sess.evict(entityToUpdate);
        sess.update(entityToUpdate);
    } catch (Exception cause) {
        throw new PersistenceException("Update of entity failed", cause);
    }
}

From source file:com.mercatis.lighthouse3.persistence.status.hibernate.StatusRegistryImplementation.java

License:Apache License

@Override
public void delete(Status statusToDelete) {
    Status status = this.findByCode(statusToDelete.getCode());
    if (status == null) {
        throw new PersistenceException("Entity not persistent", null);
    }/*from  ww  w . j  ava2 s  . co m*/
    Session session = unitOfWork.getCurrentSession();

    try {
        // null potential foreign key references
        session.createSQLQuery("delete from STATUS_METADATA where MD_STATUS_ID = :status_id")
                .setParameter("status_id", status.getId()).executeUpdate();
        session.createSQLQuery("update STATUS set LATEST_STATUS_CHANGE = null where STA_ID = :id")
                .setLong("id", status.getId()).executeUpdate();
        session.createSQLQuery("update STATUS_CHANGES set NEXT_STATUS_CHANGE = null where SCH_STATUS_ID = :id")
                .setLong("id", status.getId()).executeUpdate();
        session.createSQLQuery(
                "update STATUS_CHANGES set PREVIOUS_STATUS_CHANGE = null where SCH_STATUS_ID = :id")
                .setLong("id", status.getId()).executeUpdate();

        session.createSQLQuery("delete from STATUS_CHANGES where SCH_STATUS_ID = :id")
                .setLong("id", status.getId()).executeUpdate();
        session.createSQLQuery("delete from STATUS_NOTIFICATION_CHANNELS where SNC_STATUS_ID = :id")
                .setLong("id", status.getId()).executeUpdate();
        session.createSQLQuery("delete from STATUS where STA_ID = :id").setLong("id", status.getId())
                .executeUpdate();
    } catch (Exception e) {
        throw new PersistenceException("Deletion of status failed", e);
    }

    session.evict(status);
}

From source file:com.nec.crud.CrudRepositoryImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override
public void evict(final Session session, Object obj) {
    session.evict(obj);
}

From source file:com.qa.onlinebanking.dao.ObjectDao.java

public void updateObject(Object object, long id, Class<T> ClassName)
        throws IllegalAccessException, InvocationTargetException {
    Session session = factory.openSession();
    Transaction tx = null;//from ww  w.ja v a  2s  .c om
    try {
        session.evict(object);
        tx = session.beginTransaction();
        // Update Object

        this.t = (T) session.get(ClassName, id);
        BeanUtils.copyProperties(t, object);
        session.merge(t);
        //session.update(object);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
}

From source file:com.redhat.rhn.common.hibernate.HibernateFactory.java

License:Open Source License

/**
 * Util to reload an object using Hibernate
 * @param obj to be reloaded/*from w w  w . j  a  va  2 s.co  m*/
 * @return Object found if not, null
 * @throws HibernateException if something bad happens.
 */
public static Object reload(Object obj) throws HibernateException {
    // assertNotNull(obj);
    ClassMetadata cmd = connectionManager.getMetadata(obj);
    Serializable id = cmd.getIdentifier(obj, EntityMode.POJO);
    Session session = getSession();
    session.flush();
    session.evict(obj);
    /*
     * In hibernate 3, the following doesn't work:
     * session.load(obj.getClass(), id);
     * load returns the proxy class instead of the persisted class, ie,
     * Filter$$EnhancerByCGLIB$$9bcc734d_2 instead of Filter.
     * session.get is set to not return the proxy class, so that is what we'll use.
     */
    Object result = session.get(obj.getClass(), id);
    // assertNotSame(obj, result);
    return result;
}

From source file:com.redhat.rhn.domain.config.test.ConfigurationFactoryTest.java

License:Open Source License

public void testRemoveConfigChannel() {

    //Let's create a channel/file/revision
    ConfigRevision cr = ConfigTestUtils.createConfigRevision(user.getOrg());
    ConfigChannel channel = cr.getConfigFile().getConfigChannel();
    assertNotNull(cr.getId());/*w w w  . jav  a2s.c o m*/

    //now let's create another file in this channel without a revision,
    //just to test things out.
    ConfigFile file = ConfigTestUtils.createConfigFile(channel);
    assertNotNull(file.getId());

    //We have to evict everything from the session so that hibernate
    //doesn't complain that we removed things out from under its feet.
    Session session = HibernateFactory.getSession();
    session.flush();
    session.evict(channel);
    session.evict(file);
    session.evict(cr.getConfigFile());
    session.evict(cr);

    //run the method we are testing
    ConfigurationFactory.removeConfigChannel(channel);

    //confirm that the channel is gone.
    assertNull(ConfigurationFactory.lookupConfigChannelById(channel.getId()));
    //and everything that was in the channel.
    assertNull(ConfigurationFactory.lookupConfigFileById(file.getId()));
    assertNull(ConfigurationFactory.lookupConfigFileById(cr.getConfigFile().getId()));
    assertNull(ConfigurationFactory.lookupConfigRevisionById(cr.getId()));
}