Example usage for org.hibernate Session merge

List of usage examples for org.hibernate Session merge

Introduction

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

Prototype

Object merge(Object object);

Source Link

Document

Copy the state of the given object onto the persistent object with the same identifier.

Usage

From source file:io.jeandavid.projects.vod.entities.DvdOrder.java

License:Open Source License

public void addDvd(Dvd dvd, int quantity, Session session) {
    DvdOrderDvd temp = new DvdOrderDvd();
    temp.setQuantity(quantity);//  w w  w .  j  ava  2 s  .com
    session.persist(temp);
    dvd.addDvdOrderDvd(temp);
    this.addDvdOrderDvd(temp);
    session.saveOrUpdate(session.merge(temp));
    session.saveOrUpdate(session.merge(dvd));
    float price = temp.computePrice();
    temp.setPrice(price);
    session.saveOrUpdate(session.merge(temp));
    this.price = new Float(this.price + price);
    session.saveOrUpdate(session.merge(this));
}

From source file:io.jeandavid.projects.vod.entities.DvdOrder.java

License:Open Source License

public void removeDvd(Dvd dvd, int quantity, Session session) {
    DvdOrderDvd temp = null;/*from   www .j av a2 s .  co m*/
    for (DvdOrderDvd dvdOrderDvd : this.dvdOrderDvds) {
        if (dvdOrderDvd.getDvd().equals(dvd)) {
            temp = dvdOrderDvd;
        }
    }
    if (temp == null)
        return;

    if (temp.getQuantity() <= quantity) {
        session.delete(temp);
    } else {
        temp.setQuantity(temp.getQuantity() - quantity);
        this.price -= (temp.getPrice() - temp.computePrice());
        temp.setPrice(temp.computePrice());
        session.merge(temp);
    }
}

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

License:Open Source License

@POST
@Path("{id}/arrival")
@Consumes(MediaType.APPLICATION_JSON)/*from   w ww  . j  a  v a2 s  .  c  o m*/
public void addArrival(@PathParam("id") Long id, Map<String, Object> requestBody) throws NamingException {
    Dvd dvd = super.find(id);
    dvd.setQuantity(dvd.getQuantity() + (int) requestBody.get("quantity"));
    Session session = em.unwrap(Session.class);
    Set<Long> ids = new TreeSet<>();
    for (DvdOrderDvd dvdOrderDvd : dvd.getDvdOrderDvds()) {
        ids.add(dvdOrderDvd.getDvdOrder().getId());
    }
    Criteria crit = session.createCriteria(DvdOrder.class);
    List<DvdOrder> dvdOrders = crit.add(Restrictions.in("id", ids.toArray()))
            .add(Restrictions.like("internalState", DvdOrder.PENDING)).addOrder(Order.desc("created")).list();
    for (DvdOrder dvdOrder : dvdOrders) {
        dvdOrderSessionBean.doThePackaging(dvdOrder);
        session.merge(dvdOrder);
        session.flush();
        dvdOrderSessionBean.refreshParentDvdOrderStatus(dvdOrder.getParentDvdOrder());
    }
}

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

License:Open Source License

@POST
@Path("{id}/dvd")
@Consumes(MediaType.APPLICATION_JSON)//from ww  w .  j  a v  a 2  s.  co m
public void addDvd(@PathParam("id") Long id, Dvd dvd) {
    Session session = this.getSessionFactory().openSession();
    DvdOrder order = (DvdOrder) session.load(DvdOrder.class, id, LockMode.PESSIMISTIC_WRITE);
    if (order.getInternalState() != DvdOrder.CREATED) {
        session.close();
        return;
    }
    int quantity = dvd.getQuantity();
    session.refresh(dvd);
    Transaction tr = session.beginTransaction();

    session.refresh(order);
    DvdOrderDvd temp = new DvdOrderDvd();
    temp.setQuantity(quantity);
    session.persist(temp);
    dvd.addDvdOrderDvd(temp);
    order.addDvdOrderDvd(temp);
    session.saveOrUpdate(temp);
    session.saveOrUpdate(dvd);
    float price = temp.computePrice();
    temp.setPrice(price);
    session.saveOrUpdate(temp);
    order.updatePrice(order.getPrice() + price);
    session.save(session.merge(order));
    session.flush();
    tr.commit();
    session.close();
}

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

License:Open Source License

@POST
@Path("{id}/payment")
@Produces(MediaType.APPLICATION_JSON)/*w ww . j  av a2 s. com*/
@Consumes(MediaType.APPLICATION_JSON)
public DvdOrder pay(@PathParam("id") Long id, Map<String, Object> requestBody) {
    DvdOrder order = super.find(id);
    if (order.getInternalState() < DvdOrder.PAID) {
        Session session = this.getSessionFactory().openSession();
        Transaction tr = session.getTransaction();
        tr.begin();
        order.pay();
        session.saveOrUpdate(session.merge(order));
        session.flush();
        tr.commit();
        session.close();
        this.transformIntoSubOrders(order);
    }
    return order;
}

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

License:Open Source License

@Asynchronous
public void transformIntoSubOrders(DvdOrder dvdOrder) {
    Session rootSession = em.unwrap(Session.class);
    rootSession.refresh(dvdOrder);/*w  ww .j  a  v a2 s .  co m*/
    HashMap<DvdProvider, List<Dvd>> split = dvdOrder.sortByDvdProvider();
    for (Entry<DvdProvider, List<Dvd>> entry : split.entrySet()) {
        Session session = this.getSessionFactory().openSession();
        Transaction tr = session.beginTransaction();
        DvdOrder subOrder = new DvdOrder();
        subOrder.switchInternalState(DvdOrder.PENDING);
        session.persist(subOrder);
        subOrder.setDvdProvider(entry.getKey());
        dvdOrder.addSubOrder(subOrder);
        for (Dvd dvd : entry.getValue()) {
            for (DvdOrderDvd dvdOrderDvd : new HashSet<>(dvdOrder.getDvdOrderDvds())) {
                if (dvdOrderDvd.getDvd().equals(dvd)) {
                    subOrder.addDvdOrderDvd(dvdOrderDvd);
                    session.saveOrUpdate(session.merge(dvdOrderDvd));
                    dvdOrder.getDvdOrderDvds().remove(dvdOrderDvd);
                    rootSession.save(dvdOrder);
                }
            }
        }
        subOrder.computePrice();
        session.persist(subOrder);
        session.flush();
        tr.commit();
        session.close();
        doThePackaging(subOrder);
    }
    this.refreshParentDvdOrderStatus(dvdOrder);
}

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;/*from  w  ww. ja  v a  2 s  . c o  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:it.begear.dao.GenericDaoImpl.java

@Override
public void merge(Object object) {
    Session hibernateSession = this.getSession();
    Transaction tx = hibernateSession.beginTransaction();
    hibernateSession.merge(object);
    tx.commit();/*from   www.  j  a v a2s . co  m*/
}

From source file:it.infn.ct.security.actions.ActivateAccount.java

License:Apache License

@Override
public String execute() throws Exception {
    LDAPUser adminUser = (LDAPUser) ActionContext.getContext().getSession().get("ldapUser");

    SessionFactory factory = (SessionFactory) ServletActionContext.getServletContext()
            .getAttribute("IDPPublic.hibernatefactory");
    Session session = factory.openSession();
    session.beginTransaction();/*www . j  a  v a 2  s  .c om*/
    UserReActivateRequest urer = (UserReActivateRequest) session.get(UserReActivateRequest.class,
            Long.parseLong(id));
    if (action.equals("activate")) {
        LDAPUtils.enableUser(urer.getUsername());
        urer.setApproved(true);
        _log.info("Account for the user " + urer.getUsername() + " activated by " + adminUser.getUsername());
        sendMail(LDAPUtils.getUser(urer.getUsername()), true);
    }
    if (action.equals("delete")) {
        _log.info("Reject activation for the account of the user " + urer.getUsername() + " by "
                + adminUser.getUsername());
        sendMail(LDAPUtils.getUser(urer.getUsername()), false);
    }

    urer.setApproved(false);
    urer.setOpen(false);

    urer.setHandledBy(adminUser.getUsername());
    session.merge(urer);
    session.getTransaction().commit();
    session.close();

    return SUCCESS;
}

From source file:model.AtendimentoModel.java

public String update(Atendimento obj) {
    //Abre um sesso
    Session objSession = this.objSessionFactory.openSession();
    //Inicia uma transao dentro da sesso aberta
    Transaction objTransaction = objSession.beginTransaction();

    try {// w w w  .j  a  va  2s .co  m
        //ATUALIZA o objeto categoria, assim o hibernate persiste no bancoapagando o registro.
        objSession.merge(obj);
        //Realiza um commit do UPDATE
        objTransaction.commit();
    } catch (Exception e) {
        //Caso ocorrer algum erro, mostra uma mensagem
        msg = e.getMessage();
        //Realiza o Rollback, cancelando o UPDATE no banco de dados.
        objTransaction.rollback();
    }

    //Fecha a sesso
    objSession.close();
    //Retorna a mensagem
    return msg;
}