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:org.openmrs.module.metadatasharing.api.db.hibernate.HibernateMetadataDAO.java

License:Open Source License

private void filter(Class<?> type, Criteria criteria, boolean includeRetired, String filter) {
    if (!includeRetired) {
        criteria.add(Restrictions.eq("retired", includeRetired));
    }/*w  ww . j a v  a  2 s.  c  o m*/

    if (filter != null && !filter.isEmpty()) {
        Disjunction or = Restrictions.disjunction();
        criteria.add(or);

        or.add(Restrictions.like("uuid", filter, MatchMode.START));

        or.add(Restrictions.idEq(asItemId(filter)));

        type = ClassUtil.getDeproxiedClass(type);

        if (Role.class.isAssignableFrom(type)) {
            or.add(Restrictions.ilike("role", filter, MatchMode.START));

        } else if (Privilege.class.isAssignableFrom(type)) {
            or.add(Restrictions.ilike("privilege", filter, MatchMode.START));

        } else if (RelationshipType.class.isAssignableFrom(type)) {
            or.add(Restrictions.sqlRestriction("CONCAT(a_Is_To_B, CONCAT('/', b_Is_To_A)) like (?)",
                    "%" + filter, new StringType()));
        } else if (type.getSimpleName().equals("HtmlForm")) {
            criteria.createAlias("form", "form");
            or.add(Restrictions.ilike("form.name", filter, MatchMode.START));
        } else if (OpenmrsMetadata.class.isAssignableFrom(type)) {
            //It may happen that the name property is not defined for the specific metadata type so we need to test it.
            String[] propertyNames = sessionFactory.getClassMetadata(type).getPropertyNames();
            if (Arrays.asList(propertyNames).contains("name")) {
                or.add(Restrictions.ilike("name", filter, MatchMode.START));
            }
        }
    }
}

From source file:org.openmrs.module.sync.api.db.hibernate.HibernateSyncDAO.java

License:Open Source License

public <T extends OpenmrsObject> T getOpenmrsObjectByPrimaryKey(String classname, Object primaryKey) {
    Criteria crit;//from  w  ww. j  a  va2s  . co  m
    try {
        crit = sessionFactory.getCurrentSession().createCriteria(Context.loadClass(classname));
        crit.add(Restrictions.idEq(primaryKey));
        return (T) crit.uniqueResult();
    } catch (ClassNotFoundException e) {
        log.warn("getOpenmrsObjectByPrimaryKey couldn't find class: " + classname, e);
        return null;
    }
}

From source file:org.openmrs.module.sync.api.db.hibernate.HibernateSyncDAO.java

License:Open Source License

public <T extends OpenmrsObject> String getUuidForOpenmrsObject(Class<T> clazz, String id) {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(clazz);
    crit.add(Restrictions.idEq(id));
    crit.setProjection(Projections.property("uuid"));
    List<Object[]> rows = crit.list();
    Object[] rowOne = rows.get(0);
    return (String) rowOne[0]; // get the first column of the first row
}

From source file:org.opennms.web.svclayer.support.DefaultNodeListService.java

License:Open Source License

private static void addCriteriaForNodeId(OnmsCriteria criteria, int nodeId) {
    criteria.add(Restrictions.idEq(nodeId));
}

From source file:org.opensafety.hishare.dao.implementation.HibernateParcelDao.java

License:Apache License

public Parcel getById(String id) {
    return (Parcel) getSession().createCriteria(Parcel.class).add(Restrictions.idEq(id)).uniqueResult();
}

From source file:org.opensafety.hishare.dao.implementation.HibernateParcelDao.java

License:Apache License

public boolean verifyParcelAvailable(String parcelId) {
    return getSession().createCriteria(Parcel.class).add(Restrictions.idEq(parcelId)).uniqueResult() != null;
}

From source file:org.opensingular.lib.support.persistence.BaseDAO.java

License:Apache License

@Nonnull
public Optional<T> find(@Nonnull ID id) {
    Objects.requireNonNull(id);// w  w w  . j  ava2  s  . c  o  m
    return Optional.ofNullable((T) getSession().createCriteria(tipo).add(Restrictions.idEq(id)).uniqueResult());
}

From source file:org.opensixen.dev.omvc.server.RemoteConsole.java

License:GNU General Public License

@Override
public <T extends IPO> T load(Class<T> clazz, int id) {

    if (!Sentinel.checkAccess(PermissionFactory.get(OMVCPermission.PERM_LOADPO))) {
        throw new SecurityException("Not enough privileges.");
    }// w  ww . j av  a2s . co  m

    Criteria crit = HSession.getCriteria(clazz);
    crit.add(Restrictions.idEq(id));
    return (T) crit.uniqueResult();
}

From source file:org.opensixen.dev.omvc.util.HSession.java

License:GNU General Public License

/**
 * Devuelve el objeto con ID dado//  www.j av a 2s  . c o m
 * @param <T>
 * @param clazz
 * @param id
 * @return
 */
public static <T extends IPO> T get(Class<T> clazz, int id) {
    Criteria crit = getCriteria(clazz);
    crit.add(Restrictions.idEq(new Integer(id)));
    return (T) crit.uniqueResult();
}

From source file:org.openspaces.persistency.hibernate.StatelessHibernateExternalDataSource.java

License:Open Source License

protected boolean exists(BulkItem bulkItem, StatelessSession session) {

    Criteria criteria = null;/*from ww  w.java2  s  .  c  om*/
    switch (bulkItem.getOperation()) {
    case BulkItem.REMOVE:
    case BulkItem.WRITE:
    case BulkItem.UPDATE:
        Object entry = bulkItem.getItem();
        criteria = session.createCriteria(entry.getClass().getName());
        ClassMetadata classMetaData = getSessionFactory().getClassMetadata(entry.getClass());
        criteria.add(Restrictions.idEq(classMetaData.getIdentifier(entry)));
        criteria.setProjection(Projections.rowCount());
        return ((Number) criteria.uniqueResult()).intValue() > 0;
    case BulkItem.PARTIAL_UPDATE:
        criteria = session.createCriteria(bulkItem.getTypeName());
        criteria.add(Restrictions.idEq(bulkItem.getIdPropertyValue()));
        criteria.setProjection(Projections.rowCount());
        return ((Number) criteria.uniqueResult()).intValue() > 0;
    default:
        return false;
    }
}