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.jahia.modules.ugp.showcase.DbUserGroupProvider.java

License:Open Source License

@Override
public JahiaGroup getGroup(String name) throws GroupNotFoundException {
    JahiaGroup foundGroup = null;//from ww  w.j  ava2s  .c o  m
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();

    try {
        @SuppressWarnings("unchecked")
        List<Group> groups = hib.createCriteria(Group.class).add(Restrictions.idEq(name)).list();

        foundGroup = !groups.isEmpty() ? toGroup(groups.iterator().next()) : null;
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    if (foundGroup == null) {
        throw new GroupNotFoundException("Unable to find group " + name + " for the provider " + getKey());
    }

    return foundGroup;
}

From source file:org.jahia.modules.ugp.showcase.DbUserGroupProvider.java

License:Open Source License

@Override
public List<Member> getGroupMembers(String groupName) {
    List<Member> members = null;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();// www .j a v  a2s.co m

    try {

        @SuppressWarnings("unchecked")
        Set<Group> groups = new LinkedHashSet<>(hib.createCriteria(Group.class)
                .add(Restrictions.idEq(groupName)).setFetchMode("members", FetchMode.JOIN).list());
        if (groups.size() > 0) {
            members = new LinkedList<>();
            for (GroupMember m : groups.iterator().next().getMembers()) {
                members.add(new Member(m.getName(), MemberType.USER));
            }
        }
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    return members != null ? members : Collections.<Member>emptyList();
}

From source file:org.jahia.modules.ugp.showcase.DbUserProvider.java

License:Open Source License

@Override
public JahiaUser getUser(String name) throws UserNotFoundException {
    JahiaUser foundUser = null;//  ww w  . ja va2 s .c  o  m
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();

    try {
        @SuppressWarnings("unchecked")
        Set<User> users = new LinkedHashSet<>(hib.createCriteria(User.class).add(Restrictions.idEq(name))
                .setFetchMode("properties", FetchMode.JOIN).list());

        foundUser = !users.isEmpty() ? toUser(users.iterator().next()) : null;
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    if (foundUser == null) {
        throw new UserNotFoundException("Unable to find user " + name + " for the provider " + getKey());
    }

    return foundUser;
}

From source file:org.jpos.gl.GLSession.java

License:Open Source License

/**
 * AccountDetail for date range/*  w w w  .j a  v a2  s. c o  m*/
 * @param journal the journal.
 * @param acct the account.
 * @param start date (inclusive).
 * @param end date (inclusive).
 * @return Account detail for given period.
 * @throws GLException if user doesn't have READ permission on this jounral.
 */
public AccountDetail getAccountDetail(Journal journal, Account acct, Date start, Date end, short[] layers)
        throws HibernateException, GLException {
    checkPermission(GLPermission.READ);
    start = Util.floor(start);
    end = Util.ceil(end);

    Criteria crit = session.createCriteria(GLEntry.class);

    boolean hasChildren = false;
    if (acct instanceof CompositeAccount) {
        Disjunction dis = Restrictions.disjunction();
        for (Long l : getChildren(acct)) {
            hasChildren = true;
            dis.add(Restrictions.idEq(l));
        }
        if (hasChildren) {
            Criteria subCrit = crit.createCriteria(("account"));
            subCrit.add(dis);
        }
    }
    if (!hasChildren) {
        crit.add(Restrictions.eq("account", acct));
    }

    crit.add(Restrictions.in("layer", toShortArray(layers)));
    crit = crit.createCriteria("transaction").add(Restrictions.eq("journal", journal))
            .add(Restrictions.ge("postDate", start)).add(Restrictions.le("postDate", end));

    BigDecimal initialBalance[] = getBalances(journal, acct, start, false, layers, 0L);
    crit.addOrder(Order.asc("postDate"));
    crit.addOrder(Order.asc("timestamp"));
    crit.addOrder(Order.asc("id"));
    List entries = crit.list();
    // BigDecimal finalBalance = applyEntries (initialBalance[0], entries);

    return new AccountDetail(journal, acct, initialBalance[0], start, end, entries, layers);
}

From source file:org.libreplan.business.common.daos.GenericDAOHibernate.java

License:Open Source License

public void checkVersion(E entity) {

    /* Get id and version from entity */
    Serializable id;//from  w  ww. j a v  a2  s.  co  m
    Long versionValueInMemory;

    try {

        Method getIdMethod = entityClass.getMethod("getId");
        id = (Serializable) getIdMethod.invoke(entity);

        if (id == null) {
            return;
        }

        Method getVersionMethod = entityClass.getMethod("getVersion");
        versionValueInMemory = (Long) getVersionMethod.invoke(entity);

        if (versionValueInMemory == null) {
            return;
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    /* Check version */
    Long versionValueInDB = (Long) getSession().createCriteria(entityClass).add(Restrictions.idEq(id))
            .setProjection(Projections.property("version")).uniqueResult();

    if (versionValueInDB == null) {
        return;
    }

    if (!versionValueInMemory.equals(versionValueInDB)) {
        throw new StaleObjectStateException(entityClass.getName(), id);
    }

}

From source file:org.libreplan.business.common.daos.GenericDAOHibernate.java

License:Open Source License

public boolean exists(final PK id) {

    return getSession().createCriteria(entityClass).add(Restrictions.idEq(id)).setProjection(Projections.id())
            .uniqueResult() != null;/*from www  .  java 2 s.c  o  m*/

}

From source file:org.libreplan.business.workreports.daos.WorkReportLineDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from   w ww.  j a  v a  2s. c  om
public List<WorkReportLine> findByOrderElement(OrderElement orderElement) {
    Criteria c = getSession().createCriteria(WorkReportLine.class).createCriteria("orderElement");
    c.add(Restrictions.idEq(orderElement.getId()));
    return (List<WorkReportLine>) c.list();
}

From source file:org.molasdin.wbase.hibernate.BasicHibernateRepository.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public T byId(final K id) {
    return support().run(new org.molasdin.wbase.transaction.Transactional<HibernateEngine, T>() {
        @Override//from w ww  . j  a va  2 s. c  o m
        public T run(Transaction<HibernateEngine> tx) throws Exception {
            Session session = tx.engine().session();
            return (T) session.createCriteria(clazz).add(Restrictions.idEq(id)).uniqueResult();
        }
    });
}

From source file:org.mzd.shap.domain.dao.FeatureDaoSpringHibernate.java

License:Open Source License

public Feature findBySequenceAndId(Sequence sequence, Integer id) {
    return findUniqueByCriteria(Restrictions.eq("sequence", sequence), Restrictions.idEq(id));
}

From source file:org.mzd.shap.domain.dao.FeatureDaoSpringHibernate.java

License:Open Source License

public Feature loadWithData(final Integer id) {
    return getHibernateTemplate().execute(new HibernateCallback<Feature>() {
        public Feature doInHibernate(Session session) throws HibernateException, SQLException {
            return (Feature) session.createCriteria(getPersistentClass()).setFetchMode("data", FetchMode.JOIN)
                    .add(Restrictions.idEq(id)).uniqueResult();
        }/*w  w  w .  j  a  v  a 2s  .  co m*/
    });
}