Example usage for org.hibernate LockOptions READ

List of usage examples for org.hibernate LockOptions READ

Introduction

In this page you can find the example usage for org.hibernate LockOptions READ.

Prototype

LockOptions READ

To view the source code for org.hibernate LockOptions READ.

Click Source Link

Document

Represents LockMode.READ (timeout + scope do not apply).

Usage

From source file:com.github.jmnarloch.hstreams.internal.SessionDelegateTest.java

License:Apache License

@Test
public void testGet2() throws Exception {

    // given/*from  w w w.  ja  v a  2 s .co m*/
    final Class<?> clazz = Object.class;
    final long id = 1l;
    final LockOptions lockOptions = LockOptions.READ;

    // then
    verifyMethodCall(s -> s.get(clazz, id, lockOptions));
}

From source file:com.github.jmnarloch.hstreams.internal.SessionDelegateTest.java

License:Apache License

@Test
public void testGet5() throws Exception {

    // given/*from   w w  w.  jav  a2s  .c  o m*/
    final String entityName = "entityName";
    final long id = 1l;
    final LockOptions lockOptions = LockOptions.READ;

    // then
    verifyMethodCall(s -> s.get(entityName, id, lockOptions));
}

From source file:com.intuit.tank.dao.BaseDao.java

License:Open Source License

/**
 * Gets an entity by the id or null if no entity exists with the specified id.
 * // w  ww  . j a  va  2 s.  c o m
 * @param id
 *            the primary key
 * @return the entity or null
 */
@Nullable
public T_ENTITY findById(@Nonnull Integer id) {
    T_ENTITY result = null;
    try {
        begin();
        result = getEntityManager().find(entityClass, id);
        if (reloadEntities && result != null) {
            getHibernateSession().refresh(result, LockOptions.READ);
        }
        commit();
    } finally {
        cleanup();
    }
    return result;
}

From source file:com.intuit.tank.dao.JobQueueDao.java

License:Open Source License

/**
 * //from   ww  w. j av a 2 s  .c o  m
 * @param projectId
 * @return
 */
public synchronized JobQueue findOrCreateForProjectId(@Nonnull int projectId) {
    String prefix = "x";
    JobQueue result = null;
    NamedParameter parameter = new NamedParameter(JobQueue.PROPERTY_PROJECT_ID, "pId", projectId);
    StringBuilder sb = new StringBuilder();
    sb.append(buildQlSelect(prefix)).append(startWhere())
            .append(buildWhereClause(Operation.EQUALS, prefix, parameter));
    List<JobQueue> resultList = super.listWithJQL(sb.toString(), parameter);
    if (resultList.size() > 0) {
        result = resultList.get(0);
    }
    if (resultList.size() > 1) {
        LOG.warn("Have " + resultList.size() + " queues for project " + projectId);
    }
    if (result == null) {
        result = new JobQueue(projectId);
        result = saveOrUpdate(result);
    } else {
        getHibernateSession().refresh(result, LockOptions.READ);
    }
    return result;
}

From source file:net.derquinse.common.orm.hib.AbstractHibernateSequenceDAO.java

License:Apache License

/**
 * Gets the current value of a sequence.
 * @param id Sequence name./*from w w w  .  jav  a 2 s.  co  m*/
 * @return The current value of the specified sequence.
 * @throws SequenceNotFoundException if the sequence is not found.
 */
public long getCurrentValue(final String id) throws SequenceNotFoundException {
    nonNull(id);
    final Session session = sessionFactory.getCurrentSession();
    @SuppressWarnings("unchecked")
    final T sequence = (T) session.get(persistentClass, id, LockOptions.READ);
    if (sequence == null) {
        throw new SequenceNotFoundException(id);
    }
    return sequence.getCurrent();
}

From source file:net.sf.derquinsej.hib3.seq.SequenceDAO.java

License:Apache License

/**
 * Gets the current value of a sequence.
 * @param id Sequence name./*from   w w  w. j a v a2s  .co m*/
 * @return The current value of the specified sequence.
 * @throws SequenceNotFoundException if the sequence is not found.
 */
public long getCurrentValue(final String id) throws SequenceNotFoundException {
    nonNull(id);
    final Session session = sessionFactory.getCurrentSession();
    final Sequence sequence = (Sequence) session.get(Sequence.class, id, LockOptions.READ);
    if (sequence == null) {
        throw new SequenceNotFoundException(id);
    }
    return sequence.getCurrent();
}

From source file:org.bedework.calcore.hibernate.HibSessionImpl.java

License:Apache License

/**
 * @param o//  www  . j  av  a 2 s  . c  om
 * @throws CalFacadeException
 */
@Override
public void lockRead(final Object o) throws CalFacadeException {
    if (exc != null) {
        // Didn't hear me last time?
        throw new CalFacadeException(exc);
    }

    try {
        //      sess.lock(o, LockMode.READ);
        sess.buildLockRequest(LockOptions.READ).lock(o);
    } catch (Throwable t) {
        handleException(t);
    }
}

From source file:org.projectforge.database.XmlDump.java

License:Open Source License

/**
 * Verify the imported dump.//w  ww. j a va  2  s.c  o m
 * @return Number of checked objects. This number is negative if any error occurs (at least one object wasn't imported successfully).
 */
public int verifyDump(final XStreamSavingConverter xstreamSavingConverter) {
    final SessionFactory sessionFactory = hibernate.getSessionFactory();
    Session session = null;
    boolean hasError = false;
    try {
        session = sessionFactory.openSession(EmptyInterceptor.INSTANCE);
        session.setDefaultReadOnly(true);
        int counter = 0;
        for (final Map.Entry<Class<?>, List<Object>> entry : xstreamSavingConverter.getAllObjects()
                .entrySet()) {
            final List<Object> objects = entry.getValue();
            final Class<?> entityClass = entry.getKey();
            if (objects == null) {
                continue;
            }
            for (final Object obj : objects) {
                if (HibernateUtils.isEntity(obj.getClass()) == false) {
                    continue;
                }
                final Serializable id = HibernateUtils.getIdentifier(obj);
                if (id == null) {
                    // Can't compare this object without identifier.
                    continue;
                }
                // log.info("Testing object: " + obj);
                final Object databaseObject = session.get(entityClass, id, LockOptions.READ);
                Hibernate.initialize(databaseObject);
                final boolean equals = equals(obj, databaseObject, true);
                if (equals == false) {
                    log.error("Object not sucessfully imported! xml object=[" + obj + "], data base=["
                            + databaseObject + "]");
                    hasError = true;
                }
                ++counter;
            }
        }
        for (final HistoryEntry historyEntry : xstreamSavingConverter.getHistoryEntries()) {
            final Class<?> type = xstreamSavingConverter.getClassFromHistoryName(historyEntry.getClassName());
            final Object o = session.get(type, historyEntry.getEntityId());
            if (o == null) {
                log.error("A corrupted history entry found (entity of class '" + historyEntry.getClassName()
                        + "' with id + " + historyEntry.getEntityId() + " not found: " + historyEntry);
                hasError = true;
            }
            ++counter;
        }
        if (hasError == true) {
            log.fatal(
                    "*********** A inconsistency in the import was found! This may result in a data loss or corrupted data! Please retry the import. "
                            + counter + " entries checked.");
            return -counter;
        }
        log.info("Data-base import successfully verified: " + counter + " entries checked.");
        return counter;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.projectforge.framework.persistence.database.XmlDump.java

License:Open Source License

/**
 * Verify the imported dump.//from w  w w .  java 2 s. c o  m
 * 
 * @return Number of checked objects. This number is negative if any error occurs (at least one object wasn't imported
 *         successfully).
 */
public int verifyDump(final XStreamSavingConverter xstreamSavingConverter) {
    final SessionFactory sessionFactory = hibernate.getSessionFactory();
    Session session = null;
    boolean hasError = false;
    try {
        session = HibernateCompatUtils.openSession(sessionFactory, EmptyInterceptor.INSTANCE);
        session.setDefaultReadOnly(true);
        int counter = 0;
        for (final Map.Entry<Class<?>, List<Object>> entry : xstreamSavingConverter.getAllObjects()
                .entrySet()) {
            final List<Object> objects = entry.getValue();
            final Class<?> entityClass = entry.getKey();
            if (objects == null) {
                continue;
            }
            for (final Object obj : objects) {
                if (HibernateUtils.isEntity(obj.getClass()) == false) {
                    continue;
                }
                final Serializable id = HibernateUtils.getIdentifier(obj);
                if (id == null) {
                    // Can't compare this object without identifier.
                    continue;
                }
                // log.info("Testing object: " + obj);
                final Object databaseObject = session.get(entityClass, id, LockOptions.READ);
                Hibernate.initialize(databaseObject);
                final boolean equals = equals(obj, databaseObject, true);
                if (equals == false) {
                    log.error("Object not sucessfully imported! xml object=[" + obj + "], data base=["
                            + databaseObject + "]");
                    hasError = true;
                }
                ++counter;
            }
        }

        for (final HistoryEntry historyEntry : xstreamSavingConverter.getHistoryEntries()) {
            final Class<?> type = xstreamSavingConverter.getClassFromHistoryName(historyEntry.getEntityName());
            final Object o = type != null ? session.get(type, historyEntry.getEntityId()) : null;
            if (o == null) {
                log.warn("A corrupted history entry found (entity of class '" + historyEntry.getEntityName()
                        + "' with id " + historyEntry.getEntityId() + " not found: " + historyEntry
                        + ". This doesn't affect the functioning of ProjectForge, this may result in orphaned history entries.");
                hasError = true;
            }
            ++counter;
        }
        if (hasError == true) {
            log.fatal(
                    "*********** A inconsistency in the import was found! This may result in a data loss or corrupted data! Please retry the import. "
                            + counter + " entries checked.");
            return -counter;
        }
        log.info("Data-base import successfully verified: " + counter + " entries checked.");
        return counter;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}