Example usage for javax.persistence.metamodel EntityType getDeclaredSingularAttribute

List of usage examples for javax.persistence.metamodel EntityType getDeclaredSingularAttribute

Introduction

In this page you can find the example usage for javax.persistence.metamodel EntityType getDeclaredSingularAttribute.

Prototype

<Y> SingularAttribute<X, Y> getDeclaredSingularAttribute(String name, Class<Y> type);

Source Link

Document

Return the single-valued attribute declared by the managed type that corresponds to the specified name and Java type.

Usage

From source file:org.jboss.spring3_2.example.MatrixVariables.repo.MemberDaoImpl.java

public List<Member> findByNameAndEmail(String name, String email) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = builder.createQuery(Member.class);
    EntityType<Member> type = em.getMetamodel().entity(Member.class);
    Root<Member> member = criteria.from(Member.class);
    if (name != null && !name.isEmpty() && name != "")
        criteria.where(builder.like(member.get(type.getDeclaredSingularAttribute("name", String.class)),
                "%" + name + "%"));
    if (email != null && !email.isEmpty() && email != "")
        criteria.where(builder.like(member.get(type.getDeclaredSingularAttribute("email", String.class)),
                "%" + email + "%"));
    return em.createQuery(criteria).getResultList();
}

From source file:org.apereo.lap.dao.RiskConfidenceRepositoryImpl.java

public List<RiskConfidence> findByUserCourseDate(final String user, final String course) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<RiskConfidence> criteria = builder.createQuery(RiskConfidence.class);
    Root<RiskConfidence> root = criteria.from(RiskConfidence.class);
    EntityType<RiskConfidence> type = entityManager.getMetamodel().entity(RiskConfidence.class);
    criteria.orderBy(builder.desc(root.get("dateCreated")));

    List<RiskConfidence> lastRiskConfidences = entityManager.createQuery(criteria).setFirstResult(0)
            .setMaxResults(1).getResultList();

    if (lastRiskConfidences == null || lastRiskConfidences.isEmpty()) {
        logger.warn("No risk confidence records found");
        return new ArrayList<RiskConfidence>();
    }//from   w  w w .  j a  v  a  2s  .  co  m

    RiskConfidence lastRickConfidence = lastRiskConfidences.get(0);

    builder = entityManager.getCriteriaBuilder();
    criteria = builder.createQuery(RiskConfidence.class);
    root = criteria.from(RiskConfidence.class);
    type = entityManager.getMetamodel().entity(RiskConfidence.class);

    Predicate groupPredicate = builder.equal(root.get("groupId"), lastRickConfidence.getGroupId());

    if (!StringUtils.isEmpty(user) && !StringUtils.isEmpty(course)) {
        criteria.where(groupPredicate,
                builder.equal(
                        builder.lower(
                                root.get(type.getDeclaredSingularAttribute("alternativeId", String.class))),
                        user.toLowerCase()),
                builder.equal(
                        builder.lower(root.get(type.getDeclaredSingularAttribute("courseId", String.class))),
                        course.toLowerCase()));
    } else if (!StringUtils.isEmpty(user)) {
        criteria.where(groupPredicate,
                builder.equal(
                        builder.lower(
                                root.get(type.getDeclaredSingularAttribute("alternativeId", String.class))),
                        user.toLowerCase()));
    } else if (!StringUtils.isEmpty(course)) {
        criteria.where(groupPredicate,
                builder.equal(
                        builder.lower(root.get(type.getDeclaredSingularAttribute("courseId", String.class))),
                        course.toLowerCase()));
    } else {
        criteria.where(groupPredicate);
    }

    return entityManager.createQuery(criteria).getResultList();
}