Example usage for org.hibernate Query setLockOptions

List of usage examples for org.hibernate Query setLockOptions

Introduction

In this page you can find the example usage for org.hibernate Query setLockOptions.

Prototype

Query<R> setLockOptions(LockOptions lockOptions);

Source Link

Document

Set the lock options for the query.

Usage

From source file:com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl.java

License:Apache License

private <T extends ObjectType> PrismObject<T> getObject(Session session, Class<T> type, String oid,
        Collection<SelectorOptions<GetOperationOptions>> options, boolean lockForUpdate)
        throws ObjectNotFoundException, SchemaException, DtoTranslationException, QueryException {

    boolean lockedForUpdateViaHibernate = false;
    boolean lockedForUpdateViaSql = false;

    LockOptions lockOptions = new LockOptions();
    //todo fix lock for update!!!!!
    if (lockForUpdate) {
        if (getConfiguration().isLockForUpdateViaHibernate()) {
            lockOptions.setLockMode(LockMode.PESSIMISTIC_WRITE);
            lockedForUpdateViaHibernate = true;
        } else if (getConfiguration().isLockForUpdateViaSql()) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Trying to lock object " + oid + " for update (via SQL)");
            }/*from   ww  w  .  j  a v a2s  . c  o  m*/
            long time = System.currentTimeMillis();
            SQLQuery q = session.createSQLQuery("select oid from m_object where oid = ? for update");
            q.setString(0, oid);
            Object result = q.uniqueResult();
            if (result == null) {
                return throwObjectNotFoundException(type, oid);
            }
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Locked via SQL (in " + (System.currentTimeMillis() - time) + " ms)");
            }
            lockedForUpdateViaSql = true;
        }
    }

    if (LOGGER.isTraceEnabled()) {
        if (lockedForUpdateViaHibernate) {
            LOGGER.trace("Getting object " + oid + " with locking for update (via hibernate)");
        } else if (lockedForUpdateViaSql) {
            LOGGER.trace("Getting object " + oid + ", already locked for update (via SQL)");
        } else {
            LOGGER.trace("Getting object " + oid + " without locking for update");
        }
    }

    GetObjectResult fullObject = null;
    if (!lockForUpdate) {
        Query query = session.getNamedQuery("get.object");
        query.setString("oid", oid);
        query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER);
        query.setLockOptions(lockOptions);

        fullObject = (GetObjectResult) query.uniqueResult();
    } else {
        // we're doing update after this get, therefore we load full object right now
        // (it would be loaded during merge anyway)
        // this just loads object to hibernate session, probably will be removed later. Merge after this get
        // will be faster. Read and use object only from fullObject column.
        // todo remove this later [lazyman]
        Criteria criteria = session.createCriteria(ClassMapper.getHQLTypeClass(type));
        criteria.add(Restrictions.eq("oid", oid));

        criteria.setLockMode(lockOptions.getLockMode());
        RObject obj = (RObject) criteria.uniqueResult();

        if (obj != null) {
            obj.toJAXB(getPrismContext(), null).asPrismObject();
            fullObject = new GetObjectResult(obj.getFullObject(), obj.getStringsCount(), obj.getLongsCount(),
                    obj.getDatesCount(), obj.getReferencesCount(), obj.getPolysCount());
        }
    }

    LOGGER.trace("Got it.");
    if (fullObject == null) {
        throwObjectNotFoundException(type, oid);
    }

    LOGGER.trace("Transforming data to JAXB type.");
    PrismObject<T> prismObject = updateLoadedObject(fullObject, type, options, session);
    validateObjectType(prismObject, type);

    return prismObject;
}

From source file:com.heimaide.server.common.persistence.BaseDao.java

License:Open Source License

/**
 * ??/* www.j  a  va2  s.  c o  m*/
 *
 * @param id
 * @return
 */
@SuppressWarnings("unchecked")
public T getWithLock(Serializable id) {
    String hql = "from " + entityClass.getSimpleName() + " where id = :p1";
    Query query = createQuery(hql, new Parameter(id));
    query.setLockOptions(LockOptions.UPGRADE);
    return (T) query.uniqueResult();
}

From source file:mx.edu.um.mateo.activos.dao.impl.ActivoDaoHibernate.java

License:Open Source License

private String getFolio(Empresa empresa) {
    Query query = currentSession().createQuery(
            "select f from FolioActivo f where f.nombre = :nombre and f.organizacion.id = :organizacionId");
    query.setString("nombre", "ACTIVOS");
    query.setLong("organizacionId", empresa.getOrganizacion().getId());
    query.setLockOptions(LockOptions.UPGRADE);
    FolioActivo folio = (FolioActivo) query.uniqueResult();
    if (folio == null) {
        folio = new FolioActivo("ACTIVOS");
        folio.setOrganizacion(empresa.getOrganizacion());
        currentSession().save(folio);/*from  w  w  w.  j a v a  2 s  .  c o m*/
        return getFolio(empresa);
    }
    folio.setValor(folio.getValor() + 1);
    java.text.NumberFormat nf = java.text.DecimalFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMinimumIntegerDigits(7);
    nf.setMaximumIntegerDigits(7);
    nf.setMaximumFractionDigits(0);
    StringBuilder sb = new StringBuilder();
    sb.append("A-");
    sb.append(empresa.getOrganizacion().getCodigo());
    sb.append(empresa.getCodigo());
    sb.append(nf.format(folio.getValor()));
    return sb.toString();
}

From source file:mx.edu.um.mateo.inventario.dao.impl.CancelacionDaoHibernate.java

License:Open Source License

@Override
public String getFolio(Almacen almacen) {
    Query query = currentSession()
            .createQuery("select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId");
    query.setString("nombre", "CANCELACION");
    query.setLong("almacenId", almacen.getId());
    query.setLockOptions(LockOptions.UPGRADE);
    Folio folio = (Folio) query.uniqueResult();
    if (folio == null) {
        folio = new Folio("CANCELACION");
        folio.setAlmacen(almacen);/* w  ww .  j av a  2 s .c  o m*/
        currentSession().save(folio);
        return getFolio(almacen);
    }
    folio.setValor(folio.getValor() + 1);
    java.text.NumberFormat nf = java.text.DecimalFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMinimumIntegerDigits(9);
    nf.setMaximumIntegerDigits(9);
    nf.setMaximumFractionDigits(0);
    StringBuilder sb = new StringBuilder();
    sb.append("C-");
    sb.append(almacen.getEmpresa().getOrganizacion().getCodigo());
    sb.append(almacen.getEmpresa().getCodigo());
    sb.append(almacen.getCodigo());
    sb.append(nf.format(folio.getValor()));
    return sb.toString();
}

From source file:mx.edu.um.mateo.inventario.dao.impl.EntradaDaoHibernate.java

License:Open Source License

private String getFolioTemporal(Almacen almacen) {
    Query query = currentSession()
            .createQuery("select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId");
    query.setString("nombre", "ENTRADA-TEMPORAL");
    query.setLong("almacenId", almacen.getId());
    query.setLockOptions(LockOptions.UPGRADE);
    Folio folio = (Folio) query.uniqueResult();
    if (folio == null) {
        folio = new Folio("ENTRADA-TEMPORAL");
        folio.setAlmacen(almacen);/*from  w  ww .jav  a2  s . c  o m*/
        currentSession().save(folio);
        currentSession().flush();
        return getFolioTemporal(almacen);
    }
    folio.setValor(folio.getValor() + 1);
    java.text.NumberFormat nf = java.text.DecimalFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMinimumIntegerDigits(9);
    nf.setMaximumIntegerDigits(9);
    nf.setMaximumFractionDigits(0);
    StringBuilder sb = new StringBuilder();
    sb.append("TE-");
    sb.append(almacen.getEmpresa().getOrganizacion().getCodigo());
    sb.append(almacen.getEmpresa().getCodigo());
    sb.append(almacen.getCodigo());
    sb.append(nf.format(folio.getValor()));
    return sb.toString();
}

From source file:mx.edu.um.mateo.inventario.dao.impl.EntradaDaoHibernate.java

License:Open Source License

private String getFolio(Almacen almacen) {
    Query query = currentSession()
            .createQuery("select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId");
    query.setString("nombre", "ENTRADA");
    query.setLong("almacenId", almacen.getId());
    query.setLockOptions(LockOptions.UPGRADE);
    Folio folio = (Folio) query.uniqueResult();
    if (folio == null) {
        folio = new Folio("ENTRADA");
        folio.setAlmacen(almacen);/*w w  w  .j  av  a 2s. c o  m*/
        currentSession().save(folio);
        return getFolio(almacen);
    }
    folio.setValor(folio.getValor() + 1);
    java.text.NumberFormat nf = java.text.DecimalFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMinimumIntegerDigits(9);
    nf.setMaximumIntegerDigits(9);
    nf.setMaximumFractionDigits(0);
    StringBuilder sb = new StringBuilder();
    sb.append("E-");
    sb.append(almacen.getEmpresa().getOrganizacion().getCodigo());
    sb.append(almacen.getEmpresa().getCodigo());
    sb.append(almacen.getCodigo());
    sb.append(nf.format(folio.getValor()));
    return sb.toString();
}

From source file:mx.edu.um.mateo.inventario.dao.impl.FacturaAlmacenDaoHibernate.java

License:Open Source License

private String getFolioTemporal(Almacen almacen) {
    Query query = currentSession()
            .createQuery("select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId");
    query.setString("nombre", "FACTURA-TEMPORAL");
    query.setLong("almacenId", almacen.getId());
    query.setLockOptions(LockOptions.UPGRADE);
    Folio folio = (Folio) query.uniqueResult();
    if (folio == null) {
        folio = new Folio("FACTURA-TEMPORAL");
        folio.setAlmacen(almacen);//from  www .  j av  a2  s .  c  o  m
        currentSession().save(folio);
        currentSession().flush();
        return getFolioTemporal(almacen);
    }
    folio.setValor(folio.getValor() + 1);
    java.text.NumberFormat nf = java.text.DecimalFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMinimumIntegerDigits(9);
    nf.setMaximumIntegerDigits(9);
    nf.setMaximumFractionDigits(0);
    StringBuilder sb = new StringBuilder();
    sb.append("TF-");
    sb.append(almacen.getEmpresa().getOrganizacion().getCodigo());
    sb.append(almacen.getEmpresa().getCodigo());
    sb.append(almacen.getCodigo());
    sb.append(nf.format(folio.getValor()));
    return sb.toString();
}

From source file:mx.edu.um.mateo.inventario.dao.impl.FacturaAlmacenDaoHibernate.java

License:Open Source License

private String getFolio(Almacen almacen) {
    Query query = currentSession()
            .createQuery("select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId");
    query.setString("nombre", "FACTURA");
    query.setLong("almacenId", almacen.getId());
    query.setLockOptions(LockOptions.UPGRADE);
    Folio folio = (Folio) query.uniqueResult();
    if (folio == null) {
        folio = new Folio("FACTURA");
        folio.setAlmacen(almacen);/*from  w w  w .  ja v  a2 s  .c  om*/
        currentSession().save(folio);
        return getFolio(almacen);
    }
    folio.setValor(folio.getValor() + 1);
    java.text.NumberFormat nf = java.text.DecimalFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMinimumIntegerDigits(9);
    nf.setMaximumIntegerDigits(9);
    nf.setMaximumFractionDigits(0);
    StringBuilder sb = new StringBuilder();
    sb.append("FA-");
    sb.append(almacen.getEmpresa().getOrganizacion().getCodigo());
    sb.append(almacen.getEmpresa().getCodigo());
    sb.append(almacen.getCodigo());
    sb.append(nf.format(folio.getValor()));
    return sb.toString();
}

From source file:mx.edu.um.mateo.inventario.dao.impl.SalidaDaoHibernate.java

License:Open Source License

private String getFolioTemporal(Almacen almacen) {
    Query query = currentSession()
            .createQuery("select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId");
    query.setString("nombre", "SALIDA-TEMPORAL");
    query.setLong("almacenId", almacen.getId());
    query.setLockOptions(LockOptions.UPGRADE);
    Folio folio = (Folio) query.uniqueResult();
    if (folio == null) {
        folio = new Folio("SALIDA-TEMPORAL");
        folio.setAlmacen(almacen);/*from  ww w. ja va2s . c  om*/
        currentSession().save(folio);
        currentSession().flush();
        return getFolioTemporal(almacen);
    }
    folio.setValor(folio.getValor() + 1);
    java.text.NumberFormat nf = java.text.DecimalFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMinimumIntegerDigits(9);
    nf.setMaximumIntegerDigits(9);
    nf.setMaximumFractionDigits(0);
    StringBuilder sb = new StringBuilder();
    sb.append("TS-");
    sb.append(almacen.getEmpresa().getOrganizacion().getCodigo());
    sb.append(almacen.getEmpresa().getCodigo());
    sb.append(almacen.getCodigo());
    sb.append(nf.format(folio.getValor()));
    return sb.toString();
}

From source file:mx.edu.um.mateo.inventario.dao.impl.SalidaDaoHibernate.java

License:Open Source License

private String getFolio(Almacen almacen) {
    Query query = currentSession()
            .createQuery("select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId");
    query.setString("nombre", "SALIDA");
    query.setLong("almacenId", almacen.getId());
    query.setLockOptions(LockOptions.UPGRADE);
    Folio folio = (Folio) query.uniqueResult();
    if (folio == null) {
        folio = new Folio("SALIDA");
        folio.setAlmacen(almacen);/* w  w  w  .j  a va2 s  .  co  m*/
        currentSession().save(folio);
        return getFolio(almacen);
    }
    folio.setValor(folio.getValor() + 1);
    java.text.NumberFormat nf = java.text.DecimalFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMinimumIntegerDigits(9);
    nf.setMaximumIntegerDigits(9);
    nf.setMaximumFractionDigits(0);
    StringBuilder sb = new StringBuilder();
    sb.append("S-");
    sb.append(almacen.getEmpresa().getOrganizacion().getCodigo());
    sb.append(almacen.getEmpresa().getCodigo());
    sb.append(almacen.getCodigo());
    sb.append(nf.format(folio.getValor()));
    return sb.toString();
}