Example usage for org.hibernate LockOptions UPGRADE

List of usage examples for org.hibernate LockOptions UPGRADE

Introduction

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

Prototype

LockOptions UPGRADE

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

Click Source Link

Document

Represents LockMode.UPGRADE (will wait forever for lock and scope of false meaning only entity is locked).

Usage

From source file:io.jeandavid.projects.vod.service.DvdOrderFacadeREST.java

License:Open Source License

public void doThePackaging(DvdOrder dvdOrder) {
    if (dvdOrder.getInternalState() != DvdOrder.PENDING)
        return;/*  w w w .j a  va 2s  . co m*/
    boolean pending = false;
    Session session = this.getSessionFactory().openSession();
    session.refresh(dvdOrder);
    dvdOrder.switchInternalState(DvdOrder.PACKAGED);
    Transaction tr = session.beginTransaction();
    TreeSet<DvdOrderDvd> sortedDvdOrderDvds = dvdOrder.getSortedDvdOrderDvds();
    for (DvdOrderDvd dvdOrderDvd : sortedDvdOrderDvds) {
        Dvd dvd = (Dvd) session.load(Dvd.class, dvdOrderDvd.getDvd().getId());
        LockRequest lockRequest = session.buildLockRequest(LockOptions.UPGRADE);
        lockRequest.lock(dvd);
        Integer occurenciesNumber = dvdOrderDvd.getQuantity();
        if (dvd.getQuantity() >= occurenciesNumber) {
            dvd.setQuantity(dvd.getQuantity() - occurenciesNumber);
            session.saveOrUpdate(session.merge(dvd));
        } else {
            pending = true;
            break;
        }
    }
    if (!pending) {
        session.saveOrUpdate(dvdOrder);
        session.flush();
        tr.commit();
    }
    session.close();
}

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   ww 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);/*from  ww  w  . j  a  v 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("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);/*ww w.  j ava  2s .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);/*from   w ww  . j a  v  a2 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("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);//  w ww  .j a  v  a  2  s.co 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);//  www.  j ava2 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 v a  2 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("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);/*from   w ww  .  j  a  v a2s .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("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();
}

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

License:Apache License

/**
 * Gets the next value of a sequence. The row is locked in the current transaction.
 * @param id Sequence name.// www .  ja  v  a2 s  .co  m
 * @return The next value of the specified sequence.
 * @throws SequenceNotFoundException if the sequence is not found.
 */
public long getNextValue(final String id) throws SequenceNotFoundException {
    nonNull(id);
    final Session session = sessionFactory.getCurrentSession();
    @SuppressWarnings("unchecked")
    final T sequence = (T) session.get(persistentClass, id, LockOptions.UPGRADE);
    if (sequence == null) {
        throw new SequenceNotFoundException(id);
    }
    final long next = sequence.getNext();
    session.update(sequence);
    return next;
}