Example usage for org.hibernate.criterion Restrictions idEq

List of usage examples for org.hibernate.criterion Restrictions idEq

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions idEq.

Prototype

public static Criterion idEq(Object value) 

Source Link

Document

Apply an "equal" constraint to the identifier property

Usage

From source file:com.hm.modelo.DAOUsu.java

public String obtenerUsuarioPorId(Integer id) throws Exception {
    SessionFactory factory = HIbernate.getSessionFactory();
    Session sesion = factory.openSession();
    Transaction tranza = sesion.beginTransaction();

    Criteria cri = sesion.createCriteria(Usu.class).add(Restrictions.idEq(id));

    Usu u = (Usu) cri.uniqueResult();//from   w  ww. ja  v a 2 s . c  o  m

    ObjectMapper mapper = new ObjectMapper();

    return mapper.writeValueAsString(u);
}

From source file:com.hm.modelo.DAOUsu.java

public String obtenerPorId(Integer id) throws Exception {
    SessionFactory factory = HIbernate.getSessionFactory();
    Session sesion = factory.openSession();
    Transaction tranza = sesion.beginTransaction();

    Criteria cri = sesion.createCriteria(Usu.class).add(Restrictions.idEq(id));
    Usu usuario = (Usu) cri.uniqueResult();

    ObjectMapper mapper = new ObjectMapper();

    tranza.commit();// w w w  . jav a2  s  . co  m
    sesion.close();

    return mapper.writeValueAsString(usuario);
}

From source file:com.hmsinc.epicenter.model.workflow.impl.WorkflowRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Subscription> getSubscriptions(Classification classification, Geography geography) {

    Validate.notNull(classification, "Classification must be specified.");
    Validate.notNull(geography, "Geography must be specified.");

    final Criteria c = criteriaQuery(entityManager, Subscription.class);
    c.createCriteria("classifications").add(Restrictions.idEq(classification.getId()));
    c.createCriteria("geographies").add(SpatialRestrictions.contains("geometry", geography.getGeometry()));
    c.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    return c.list();

}

From source file:com.ipn.escom.ageinnn.configuracion.service.PeriodoServiceImpl.java

public PeriodoAdmision findById(Integer periodoId) {
    PeriodoAdmision periodoAdmision;/* w ww . j a va  2s .  c o  m*/
    try {
        session = Service.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        periodoAdmision = (PeriodoAdmision) session.createCriteria(PeriodoAdmision.class)
                .add(Restrictions.idEq(periodoId)).uniqueResult();
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
        throw e;
    }
    return periodoAdmision;
}

From source file:com.ipn.escom.ageinnn.evento.service.EventoService.java

public Evento findByDocenteAndEventoId(Docente docente, Integer eventoId) {
    Evento entrevista = null;//from  w  ww. j a v  a  2  s.  c  o m
    try {
        session = Service.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        entrevista = (Evento) session.createCriteria(Evento.class).add(
                Restrictions.and(Restrictions.idEq(eventoId), Restrictions.eq("docenteId", docente.getId())))
                .uniqueResult();

        session.getTransaction().commit();

    } catch (Exception e) {
        session.getTransaction().rollback();
        throw e;
    }

    return entrevista;
}

From source file:com.jaspersoft.jasperserver.api.metadata.data.snapshot.hibernate.HibernateDataSnapshotService.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public boolean matchesVersion(ExecutionContext context, final long snapshotId, final int version) {
    return getHibernateTemplate().execute(new HibernateCallback<Boolean>() {
        public Boolean doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(PersistentDataSnapshot.class);
            criteria.add(Restrictions.idEq(snapshotId));
            criteria.add(Restrictions.eq("version", version));
            criteria.setProjection(Projections.rowCount());
            Number count = (Number) criteria.uniqueResult();

            if (log.isDebugEnabled()) {
                log.debug("version check for " + snapshotId + " and " + version + " returned " + count);
            }//  w  w  w  .  j ava 2 s  .c  o m

            return count.intValue() > 0;
        }
    });
}

From source file:com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.java

License:Open Source License

private DetachedCriteria createAssignedRolesCriteria(ExecutionContext context, RepoUser user, String text,
        boolean order) {
    final String roleNameField = "roleName";
    final String externallyDefinedField = "externallyDefined";

    DetachedCriteria criteria = DetachedCriteria.forClass(getPersistentRoleClass());

    DetachedCriteria usersCriteria = criteria.createCriteria("users");
    usersCriteria.add(Restrictions.idEq(user.getId()));

    final String roleNameLikeValue = text == null ? "" : text;
    Criterion roleNameCriterion = Restrictions.ilike(roleNameField, "%" + roleNameLikeValue + "%");
    criteria.add(roleNameCriterion);//  w w w.  jav  a  2 s  .c  o  m

    criteria.add(Restrictions.eq(externallyDefinedField, Boolean.FALSE));

    if (order) {
        criteria.addOrder(Order.asc(roleNameField));
    }

    return criteria;
}

From source file:com.jc.elementos.model.Insertar.java

public static void main(String args[]) {
    SessionFactory sesion = NewHibernateUtil.getSessionFactory();
    Session ses = sesion.openSession();//from www. j  a v a  2 s.c o  m
    Transaction t = ses.beginTransaction();

    Trabajador uno = (Trabajador) ses.createCriteria(Trabajador.class).add(Restrictions.idEq(1)).uniqueResult();

    t.commit();
    ses.close();

}

From source file:com.jpizarro.th.server.generic.model.persistence.accessor.GenericAccessorHibernate.java

License:Open Source License

public boolean exists(PK id) {

    return getSession().createCriteria(entityClass).add(Restrictions.idEq(id)).setProjection(Projections.id())
            .uniqueResult() != null;

}

From source file:com.klistret.cmdb.dao.ElementDAOImpl.java

License:Open Source License

/**
 * Another proxy (shallow copy of the object) is returned to avoid Hibernate
 * lazy exceptions in the RestEasy layer. A generic approach to transferring
 * properties would be better/safer to mapping/POJO changes.
 * /*from   w  ww .j  a v a 2s. c  o  m*/
 * @param id
 * @return Element
 */
public Element get(Long id) {
    try {
        logger.debug("Getting element [id: {}] by id [session: {}]", id, getSession().hashCode());

        Criteria criteria = getSession().createCriteria(Element.class);
        criteria.add(Restrictions.idEq(id));

        Element element = (Element) criteria.uniqueResult();

        if (element == null)
            throw new ApplicationException(String.format("Element [id: %s] not found", id),
                    new NoSuchElementException());

        logger.debug("Returning element by id [id: {}, name: {}, version: {}, session: {}]", new Object[] {
                element.getId(), element.getName(), element.getVersion(), getSession().hashCode() });

        return element;
    } catch (StaleStateException e) {
        throw new ApplicationException(String
                .format("Got element [id: %d] is stale which means newer version exists (Hibernate).", id));
    } catch (HibernateException e) {
        throw new InfrastructureException(e.getMessage(), e);
    }
}