Example usage for org.hibernate Session update

List of usage examples for org.hibernate Session update

Introduction

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

Prototype

void update(Object object);

Source Link

Document

Update the persistent instance with the identifier of the given detached instance.

Usage

From source file:ca.qc.cegepoutaouais.tge.pige.server.ManagementServiceImpl.java

License:Open Source License

@Override
public void updateLoans(List<Loan> loans) throws PigeException {

    PermissionHelper.checkLoanUpdatePermission(getThreadLocalRequest());

    logger.debug("Mise  jour des demandes d'emprunts...");

    Session session = null;
    Transaction tx = null;//from   ww  w  .  ja  v a2s  .com

    try {
        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();

        for (Loan l : loans) {
            session.update(l);
        }
        tx.commit();
        logger.debug("Mise  jour russie!");
    } catch (HibernateException hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }

}

From source file:ca.qc.cegepoutaouais.tge.pige.server.ManagementServiceImpl.java

License:Open Source License

@Override
public void updateItems(List<Item> items) throws PigeException {

    PermissionHelper.checkInventoryManagementPermission(this.getThreadLocalRequest());

    logger.debug("Mise  jour d'un item...");

    Session session = null;
    Transaction tx = null;//from  w w w. ja va  2 s .  co  m

    try {
        /* On enlve l'unicit des noms pour les items.
                    session = PigeHibernateUtil.openSession();
                    tx = session.beginTransaction();
                    Item existingItem = (Item) session.createCriteria(Item.class)
            .add(Restrictions.eq(Item.NAME_REF, item.getName()))
            .uniqueResult();
                    tx.commit();
                    session.close();
                
                    if (existingItem != null) {
        if (!item.getId().equals(existingItem.getId())) {
            throw new ItemCreationException(
                    "Le nom choisi est dj utilis par un autre item.");
        }
                    }
        */
        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();
        for (Item i : items) {
            session.update(i);
        }
        tx.commit();

        logger.debug("Mise  jour russie!");

    } catch (HibernateException hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }

}

From source file:ca.qc.cegepoutaouais.tge.pige.server.ManagementServiceImpl.java

License:Open Source License

@Override
public void updateMaintenance(Maintenance maintenance) throws PigeException {

    PermissionHelper.checkInventoryManagementPermission(this.getThreadLocalRequest());

    logger.debug("Mise  jour d'une maintenance...");

    Session session = null;
    Transaction tx = null;/* www.  j a v a  2  s  .  c om*/

    try {

        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();
        session.update(maintenance);
        tx.commit();

        logger.debug("Mise  jour russie!");

    } catch (HibernateException hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }

}

From source file:ca.qc.cegepoutaouais.tge.pige.server.ManagementServiceImpl.java

License:Open Source License

@Override
public void updateCategories(List<Category> categories) throws PigeException {

    logger.debug("Mise  jour d'une catgorie...");

    Transaction tx = null;/*  ww  w . j  av  a 2s .  c  o  m*/
    Session session = null;

    try {
        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();
        for (Category c : categories) {
            session.update(c);
        }
        tx.commit();
        logger.debug("Mise  jour russie!");
    } catch (Exception hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }

}

From source file:ca.qc.cegepoutaouais.tge.pige.server.ManagementServiceImpl.java

License:Open Source License

@Override
public void deleteCategory(List<Category> categories) throws PigeException {

    logger.debug("Suppression d'une ou des catgorie(s)...");

    Transaction tx = null;// w  w w .ja va 2s .  c  o  m
    Session session = null;

    try {
        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();

        for (Category c : categories) {
            // Les id ngatifs sont rservs  des catgories spciales telles
            // que la catgorie "Non-class" et ne peuvent donc pas tre
            // supprimes.
            if (c.getId() >= 0) {
                if (c.getParent() != null) {
                    Integer id = c.getParent().getId();
                    Category parent = (Category) session.createCriteria(Category.class)
                            .add(Restrictions.eq(Category.ID_REF, id)).uniqueResult();
                    parent.removeChild(c);
                    session.update(parent);
                } else {
                    session.delete(c);
                }

                logger.debug("Catgorie '" + c.getPath() + "' supprime.");
            } else {
                logger.warn("La catgorie suivante ne peut pas tre " + "supprime: " + c.toString());
            }
        }
        tx.commit();
        logger.debug("Suppression russie!");
    } catch (Exception hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:ca.qc.cegepoutaouais.tge.pige.server.UserServiceImpl.java

License:Open Source License

@Override
public void changePassword(String oldPwd, String newPwd, String confirmNewPwd)
        throws InvalidPasswordException, PasswordsMismatchException, PasswordFormatException {

    HttpSession httpSession = this.getThreadLocalRequest().getSession();

    logger.info("Changement du mot de passe du compte associ au thread " + "courrant. Session id:"
            + httpSession.getId());// ww  w.  ja va 2 s.  c o  m

    Session session = null;
    Transaction tx = null;
    try {

        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();

        User user = (User) session.createCriteria(User.class)
                .add(Restrictions.eq(User.SID_REF, httpSession.getId())).uniqueResult();

        String hashedOldPwd = ServerUtil.produceSHA1(oldPwd);
        if (!hashedOldPwd.equals(user.getPassword())) {
            logger.info("Ancien mot de passe invalide.");
            throw new InvalidPasswordException();
        }
        if (!newPwd.equals(confirmNewPwd)) {
            logger.info("Les nouveaux mots de passes ne corresponde pas.");
            throw new PasswordsMismatchException();
        }
        if (!GenericUtil.isValidPassword(newPwd) || !GenericUtil.isValidPassword(confirmNewPwd)) {
            logger.info("Le format du nouveau mot de passe n'est pas valide.");
            throw new PasswordFormatException();
        }
        String hashedNewPwd = ServerUtil.produceSHA1(newPwd);
        user.setPassword(hashedNewPwd);
        session.update(user);
        tx.commit();
        logger.info("Changement du mot de passe russie!");
    } catch (HibernateException hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }

}

From source file:ca.qc.cegepoutaouais.tge.pige.server.UserServiceImpl.java

License:Open Source License

@Override
public void updateEmail(String newEmail) {

    HttpSession httpSession = this.getThreadLocalRequest().getSession();

    logger.debug("Changement du courriel du compte associ au thread " + "courant... Session id: "
            + httpSession.getId());/*from   w  ww.  j a va  2s . com*/

    Session session = null;
    Transaction tx = null;

    try {

        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();

        User user = (User) session.createCriteria(User.class)
                .add(Restrictions.eq(User.SID_REF, httpSession.getId())).uniqueResult();

        logger.info("Mise  jour des libells du compte suivant: " + user.asString());

        user.setEmail(newEmail);
        session.update(user);
        tx.commit();
        logger.debug("Changement du courriel russi!");
    } catch (HibernateException hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:ca.ualberta.physics.cssdp.catalogue.dao.UrlDataProductDao.java

License:Apache License

public void process(UrlDataProductUpdateMap urlDataProductUpdateMap) {

    if (urlDataProductUpdateMap.getUrls().size() == 0) {
        return;// ww  w  .  j a va 2 s  .c o m
    }

    /*
     * The size of scannedUrlDataProducts should be <= jdbc batch size
     * configured.
     */

    // we have to resort to hibernate directly because JPA does not have
    // scrolling capability
    Session session = emp.get().unwrap(Session.class).getSessionFactory().openSession();

    Transaction tx = session.beginTransaction();

    // "in" clause limit is 2^16 on Postgresql, it might be different on
    // other dbs
    String hqlString = "from UrlDataProduct urldp where urldp.url in (:urls)";

    // the fastest way to scroll through the existing data
    Query q = session.createQuery(hqlString);
    q.setParameterList("urls", urlDataProductUpdateMap.getUrls());
    q.setCacheMode(CacheMode.IGNORE);
    ScrollableResults existingData = q.scroll(ScrollMode.FORWARD_ONLY);

    while (existingData.next()) {

        UrlDataProduct existing = (UrlDataProduct) existingData.get(0);
        UrlDataProduct updated = urlDataProductUpdateMap.get(existing.getUrl());

        if (updated != null) {

            /*
             * Only bother to update the record if it's actually changed.
             * Note that the scan timestamp is ignored in the check because
             * that isn't something the provider changed. A change can also
             * mean the url was deleted, and now it's back.
             */
            if (existing.hasChanged(updated)) {
                // existing.setDataProduct(updated.getDataProduct());
                existing.setUrl(updated.getUrl());
                existing.setStartTimestamp(updated.getStartTimestamp());
                existing.setEndTimestamp(updated.getEndTimestamp());
                existing.setScanTimestamp(updated.getScanTimestamp());
                existing.setDeleted(false);
                urlDataProductUpdateMap.remove(updated.getUrl());
                session.update(existing);
            } else {
                // remove it so it's not duplicated
                urlDataProductUpdateMap.remove(existing.getUrl());
            }

        } else {

            // if we get here it means the existing url has been removed
            // from the server, set "delete" it from the catalogue
            existing.setDeleted(true);
            existing.setScanTimestamp(new LocalDateTime());

        }

    }

    // persist the new url mappings
    for (String newUrl : urlDataProductUpdateMap.getUrls()) {
        UrlDataProduct newUrlDataProduct = urlDataProductUpdateMap.get(newUrl);
        session.save(newUrlDataProduct);
        logger.debug("saved a mapping: " + newUrlDataProduct.getUrl());
    }

    session.flush();
    session.clear();

    tx.commit();
    session.close();

}

From source file:ca.usask.gmcte.currimap.action.QuestionManager.java

License:Open Source License

public boolean saveQuestion(int id, String display, String questionType, int answerSetId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from   ww  w . j a va2  s  .  c o  m*/
    try {
        Question o = new Question();

        if (id > -1) {
            o = (Question) session.get(Question.class, id);
        }
        o.setDisplay(display);
        if (answerSetId > -1) {
            AnswerSet set = (AnswerSet) session.get(AnswerSet.class, answerSetId);
            o.setAnswerSet(set);
        }
        if (questionType != null) {
            QuestionType type = (QuestionType) session.createQuery("FROM QuestionType WHERE name=:name")
                    .setParameter("name", questionType).uniqueResult();
            ;
            o.setQuestionType(type);
        }
        if (id > -1)
            session.update(o);
        else
            session.save(o);
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return false;
    }
}

From source file:caipsfa.app.modelo.GestionDiagnostico.java

public boolean editDiagnostic(DiagnosticoForm diag) {
    boolean estado = false;
    try {/*from  w w  w. jav a  2 s . c o m*/
        SessionFactory sesFact = HibernateUtil.getSessionFactory();
        Session ses = sesFact.openSession();
        Transaction trans = ses.beginTransaction();
        Diagnostico diagnostic = new Diagnostico();
        diagnostic.setCodigoDiagnostico(Integer.parseInt(diag.getCodigoDiagnostico()));
        diagnostic.setNombre(diag.getNombre());
        diagnostic.setDescripcion(diag.getDescripcion());
        ses.update(diagnostic);
        trans.commit();
        ses.close();
        estado = true;
        return estado;
    } catch (Exception e) {
        System.out.println(e);
        return estado;
    }
}