Example usage for org.hibernate Session load

List of usage examples for org.hibernate Session load

Introduction

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

Prototype

void load(Object object, Serializable id);

Source Link

Document

Read the persistent state associated with the given identifier into the given transient instance.

Usage

From source file:com.daro.persistence.generic.dao.GenericDaoImpl.java

License:GNU General Public License

/**
 * Remove entity with id passed./*from  w  w  w  . j  av  a  2 s.  c  o  m*/
 * 
 * @param id Entity identifier
 * @throws PersistenceException
 */
@Override
@SuppressWarnings("unchecked")
public void removeById(Long id) throws PersistenceException {
    Session session = this.getCurrentSession();
    T t = (T) session.load(clazz, id.longValue());
    if (null != t) {
        session.delete(t);
        if (loggerInfoEnabled)
            logger.debug("Persistence layer info: " + clazz.getSimpleName() + " deleted successfully,  details="
                    + t);
    } else {
        if (loggerInfoEnabled)
            logger.debug("Persistence layer info: " + clazz.getSimpleName()
                    + " can not be deleted because is null!");
    }
}

From source file:com.dell.asm.asmcore.asmmanager.db.FirmwareRepositoryDAO.java

License:Open Source License

public FirmwareRepositoryEntity merge(final FirmwareRepositoryEntity original,
        final FirmwareRepositoryEntity update) {
    Session session = null;
    Transaction tx = null;//from ww  w  . j  ava  2 s  .  c o m

    try {
        session = dao.getNewSession();
        tx = session.beginTransaction();

        if (original != null) {
            final String originalId = original.getId();
            session.load(original, originalId);
            original.getDeployments().clear();
            original.getSoftwareBundles().clear();
            original.getSoftwareComponents().clear();
            original.getTemplates().clear();
            original.getDeviceInventoryComplianceEntities().clear();
            session.flush();
            // reload the original entity in cache so we can merge
            session.get(FirmwareRepositoryEntity.class, originalId);
            update.setId(originalId);
            update.setCreatedDate(original.getCreatedDate());
            update.setCreatedBy(original.getCreatedBy());
            update.setUpdatedDate(new Date());
            update.setUpdatedBy(dao.extractUserFromRequest());
            session.merge(update);
        } else {
            update.setCreatedDate(new Date());
            update.setCreatedBy(dao.extractUserFromRequest());
            update.setUpdatedDate(update.getCreatedDate());
            update.setUpdatedBy(update.getCreatedBy());
            session.saveOrUpdate(update);
        }

        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during merge: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during merge: " + ex);
        }
        throw new AsmManagerInternalErrorException("merge", "FirmwareRepositoryDAO", e);
    } finally {
        dao.cleanupSession(session, "merge");
    }

    return original;
}

From source file:com.dentinium.doctor.DoctorDataController.java

public void deleteDoctor(int doctorid) {
    Session sess = createSession();
    Transaction tran = null;/*from w w w  .ja  v a 2  s  .c o m*/
    try {

        tran = sess.beginTransaction();
        Doctors doc = (Doctors) sess.load(Reservations.class, doctorid);
        sess.delete(doc);
        tran.commit();

    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        sess.close();
    }

}

From source file:com.dentinium.reservation.ReservationDataController.java

public void deleteReservationForDoctor(int reservationid) {
    Session sess = createSession();
    Transaction tran = null;/*  w  ww.ja  v  a 2 s .c om*/
    try {

        tran = sess.beginTransaction();
        Reservations res = (Reservations) sess.load(Reservations.class, reservationid);
        sess.delete(res);
        tran.commit();
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_WARN, "Deleted!", ""));

    } catch (Exception ex) {
        ex.printStackTrace();
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_WARN, "Deletion Error!", ""));
    } finally {
        sess.close();
    }

}

From source file:com.duroty.application.mail.ejb.MailBean.java

License:Open Source License

/**
 * Get messages by label/*from w ww . ja  v  a2 s  . com*/
 *
 * @ejb.interface-method view-type = "remote"
 * @ejb.permission
 *         role-name = "mail"
 *
 * @return Vector
 *
 * @throws MailException Thrown if method fails due to system-level error.
 */
public Vector getMessages(int labIdint, int page, int messagesByPage, int order, String orderType)
        throws MailException {
    SessionFactory hfactory = null;
    Session hsession = null;

    try {
        hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory);

        hsession = hfactory.openSession();

        Label label = (Label) hsession.load(Label.class, new Integer(labIdint));

        String repositoryName = context.getCallerPrincipal().getName();

        return manager.getMessages(hfactory.openSession(), repositoryName, label, page, messagesByPage, order,
                orderType);
    } catch (NamingException e) {
        throw new MailException(e);
    } catch (Exception e) {
        if (e instanceof MailException) {
            throw (MailException) e;
        }

        throw new MailException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.mail.ejb.MailBean.java

License:Open Source License

/**
 * Get count messages in label/*  ww w  . j av a  2 s  . c o  m*/
 *
 * @ejb.interface-method view-type = "remote"
 * @ejb.permission
 *         role-name = "mail"
 *
 * @return Vector
 *
 * @throws MailException Thrown if method fails due to system-level error.
 */
public int getCountMessages(int labIdint) throws MailException {
    SessionFactory hfactory = null;
    Session hsession = null;

    try {
        hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory);

        hsession = hfactory.openSession();

        Label label = (Label) hsession.load(Label.class, new Integer(labIdint));

        String repositoryName = context.getCallerPrincipal().getName();

        return manager.getCountMessages(hfactory.openSession(), repositoryName, label);
    } catch (NamingException e) {
        throw new MailException(e);
    } catch (Exception e) {
        if (e instanceof MailException) {
            throw (MailException) e;
        }

        throw new MailException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.mail.manager.PreferencesManager.java

License:Open Source License

/**
 * DOCUMENT ME!// w w  w .  ja  v a2s .com
 *
 * @param hsession DOCUMENT ME!
 * @param token DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws ManagerException DOCUMENT ME!
 */
public void updateFilter(Session hsession, String repositoryName, FilterObj filter) throws MailException {
    String lucenePathMessages = null;

    if (!defaultLucenePath.endsWith(File.separator)) {
        lucenePathMessages = defaultLucenePath + File.separator + repositoryName + File.separator
                + Constants.MAIL_LUCENE_MESSAGES;
    } else {
        lucenePathMessages = defaultLucenePath + repositoryName + File.separator
                + Constants.MAIL_LUCENE_MESSAGES;
    }

    Searcher searcher = null;

    try {
        searcher = MailIndexer.getSearcher(lucenePathMessages);

        Users user = getUser(hsession, repositoryName);

        if (user != null) {
            Criteria crit1 = hsession.createCriteria(Label.class);
            crit1.add(Restrictions.eq("labIdint", filter.getLabel().getIdint()));
            crit1.add(Restrictions.eq("users", user));

            Label hlabel = (Label) crit1.uniqueResult();

            Filter hfilter = (Filter) hsession.load(Filter.class, filter.getIdint());

            hfilter.setFilArchive(filter.isArchive());
            hfilter.setFilDoesntHaveWords(filter.getDoesntHaveWords());
            hfilter.setFilForwardTo(filter.getForward());
            hfilter.setFilFrom(filter.getFrom());

            //CANVIDUROT
            hfilter.setFilHasAttacment(filter.isHasAttachment());
            hfilter.setFilHasWords(filter.getHasWords());
            hfilter.setFilImportant(filter.isImportant());
            hfilter.setFilSubject(filter.getSubject());
            hfilter.setFilTo(filter.getTo());
            hfilter.setFilTrash(filter.isTrash());
            hfilter.setFilOrOperator(!filter.isOperator());

            hfilter.setLabel(hlabel);

            hsession.update(hfilter);
            hsession.flush();

            org.apache.lucene.search.Query query = FilterQueryParser.parse(hfilter, analyzer);

            if ((searcher != null) && (query != null)) {
                Hits hits = searcher.search(query);

                for (int j = 0; j < hits.length(); j++) {
                    Document doc = hits.doc(j);
                    String uid = doc.get(Field_idint);

                    Criteria crit2 = hsession.createCriteria(Message.class);
                    crit2.add(Restrictions.eq("mesName", uid));
                    crit2.add(Restrictions.eq("users", user));

                    Message message = (Message) crit2.uniqueResult();

                    if (message != null) {
                        try {
                            LabMesId id = new LabMesId();
                            id.setLabel(hlabel);
                            id.setMessage(message);

                            LabMes lm = new LabMes(id);
                            hsession.saveOrUpdate(lm);
                            hsession.flush();
                        } catch (HibernateException e) {
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        throw new MailException(ex);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.ea.core.orm.handle.impl.LoadORMHandle.java

License:Apache License

@Override
protected Object execute(ORMParamsDTO dto) throws Exception {
    // TODO Auto-generated method stub
    Object po = dto.getParam();/*from   www .  j  a v a2  s.c o m*/
    if (po instanceof BasePO) {
        Session session = this.getHibernateSessionFactory().getCurrentSession();
        Object obj = session.load(po.getClass(), ((BasePO) po).getPk());
        return obj;
    } else {
        throw new Exception("??BasePO!");
    }
}

From source file:com.ecama.dao.ContestDAOImpl.java

@Override
public Contents getContentsbyId(int Id) {
    Session session = this.sessionFactory.getCurrentSession();
    Contents c = (Contents) session.load(Contents.class, new Integer(Id));
    return c;/*  ww w  .ja v a2 s  . co m*/
}

From source file:com.ecama.dao.FilesDAOImpl.java

@Override
public Files find(int id) {
    Session session = this.sessionFactory.getCurrentSession();
    Files f = (Files) session.load(Files.class, new Integer(id));
    return f;/*from  w ww.ja v a2 s . c om*/
}