Search in sources :

Example 1 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project hibernate-orm by hibernate.

the class EmbeddedTypeTest method testSingularAttributeAccessByName.

@Test
@TestForIssue(jiraKey = "HHH-4702")
public void testSingularAttributeAccessByName() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    SingularAttribute soldDate_ = em.getMetamodel().embeddable(ShelfLife.class).getSingularAttribute("soldDate");
    assertEquals(java.sql.Date.class, soldDate_.getBindableJavaType());
    assertEquals(java.sql.Date.class, soldDate_.getType().getJavaType());
    assertEquals(java.sql.Date.class, soldDate_.getJavaType());
    em.getTransaction().commit();
    em.close();
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project hibernate-orm by hibernate.

the class AbstractManagedType method getBuilder.

public Builder<X> getBuilder() {
    if (locked) {
        throw new IllegalStateException("Type has been locked");
    }
    return new Builder<X>() {

        @Override
        @SuppressWarnings("unchecked")
        public void addAttribute(Attribute<X, ?> attribute) {
            declaredAttributes.put(attribute.getName(), attribute);
            final Bindable.BindableType bindableType = ((Bindable) attribute).getBindableType();
            switch(bindableType) {
                case SINGULAR_ATTRIBUTE:
                    {
                        declaredSingularAttributes.put(attribute.getName(), (SingularAttribute<X, ?>) attribute);
                        break;
                    }
                case PLURAL_ATTRIBUTE:
                    {
                        declaredPluralAttributes.put(attribute.getName(), (PluralAttribute<X, ?, ?>) attribute);
                        break;
                    }
                default:
                    {
                        throw new AssertionFailure("unknown bindable type: " + bindableType);
                    }
            }
        }
    };
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) AssertionFailure(org.hibernate.annotations.common.AssertionFailure) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) SetAttribute(javax.persistence.metamodel.SetAttribute) CollectionAttribute(javax.persistence.metamodel.CollectionAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) ListAttribute(javax.persistence.metamodel.ListAttribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) Bindable(javax.persistence.metamodel.Bindable)

Example 3 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project ranger by apache.

the class XTrxLogService method generatePredicate.

private Predicate generatePredicate(SearchCriteria searchCriteria, EntityManager em, CriteriaBuilder criteriaBuilder, Root<VXXTrxLog> rootEntityType) {
    Predicate predicate = criteriaBuilder.conjunction();
    Map<String, Object> paramList = searchCriteria.getParamList();
    if (CollectionUtils.isEmpty(paramList)) {
        return predicate;
    }
    Metamodel entityMetaModel = em.getMetamodel();
    EntityType<VXXTrxLog> entityType = entityMetaModel.entity(VXXTrxLog.class);
    for (Map.Entry<String, Object> entry : paramList.entrySet()) {
        String key = entry.getKey();
        for (SearchField searchField : searchFields) {
            if (!key.equalsIgnoreCase(searchField.getClientFieldName())) {
                continue;
            }
            String fieldName = searchField.getFieldName();
            if (!StringUtils.isEmpty(fieldName)) {
                fieldName = fieldName.contains(".") ? fieldName.substring(fieldName.indexOf(".") + 1) : fieldName;
            }
            Object paramValue = entry.getValue();
            boolean isListValue = false;
            if (paramValue != null && paramValue instanceof Collection) {
                isListValue = true;
            }
            // build where clause depending upon given parameters
            if (SearchField.DATA_TYPE.STRING.equals(searchField.getDataType())) {
                // build where clause for String datatypes
                SingularAttribute attr = entityType.getSingularAttribute(fieldName);
                if (attr != null) {
                    Predicate stringPredicate = null;
                    if (SearchField.SEARCH_TYPE.PARTIAL.equals(searchField.getSearchType())) {
                        String val = "%" + paramValue + "%";
                        stringPredicate = criteriaBuilder.like(rootEntityType.get(attr), val);
                    } else {
                        stringPredicate = criteriaBuilder.equal(rootEntityType.get(attr), paramValue);
                    }
                    predicate = criteriaBuilder.and(predicate, stringPredicate);
                }
            } else if (SearchField.DATA_TYPE.INT_LIST.equals(searchField.getDataType()) || isListValue && SearchField.DATA_TYPE.INTEGER.equals(searchField.getDataType())) {
                // build where clause for integer lists or integers datatypes
                Collection<Number> intValueList = null;
                if (paramValue != null && (paramValue instanceof Integer || paramValue instanceof Long)) {
                    intValueList = new ArrayList<Number>();
                    intValueList.add((Number) paramValue);
                } else {
                    intValueList = (Collection<Number>) paramValue;
                }
                for (Number value : intValueList) {
                    SingularAttribute attr = entityType.getSingularAttribute(fieldName);
                    if (attr != null) {
                        Predicate intPredicate = criteriaBuilder.equal(rootEntityType.get(attr), value);
                        predicate = criteriaBuilder.and(predicate, intPredicate);
                    }
                }
            } else if (SearchField.DATA_TYPE.DATE.equals(searchField.getDataType())) {
                // build where clause for date datatypes
                Date fieldValue = (Date) paramList.get(searchField.getClientFieldName());
                if (fieldValue != null && searchField.getCustomCondition() == null) {
                    SingularAttribute attr = entityType.getSingularAttribute(fieldName);
                    Predicate datePredicate = null;
                    if (SearchField.SEARCH_TYPE.LESS_THAN.equals(searchField.getSearchType())) {
                        datePredicate = criteriaBuilder.lessThan(rootEntityType.get(attr), fieldValue);
                    } else if (SearchField.SEARCH_TYPE.LESS_EQUAL_THAN.equals(searchField.getSearchType())) {
                        datePredicate = criteriaBuilder.lessThanOrEqualTo(rootEntityType.get(attr), fieldValue);
                    } else if (SearchField.SEARCH_TYPE.GREATER_THAN.equals(searchField.getSearchType())) {
                        datePredicate = criteriaBuilder.greaterThan(rootEntityType.get(attr), fieldValue);
                    } else if (SearchField.SEARCH_TYPE.GREATER_EQUAL_THAN.equals(searchField.getSearchType())) {
                        datePredicate = criteriaBuilder.greaterThanOrEqualTo(rootEntityType.get(attr), fieldValue);
                    } else {
                        datePredicate = criteriaBuilder.equal(rootEntityType.get(attr), fieldValue);
                    }
                    predicate = criteriaBuilder.and(predicate, datePredicate);
                }
            }
        }
    }
    return predicate;
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) Predicate(javax.persistence.criteria.Predicate) SingularAttribute(javax.persistence.metamodel.SingularAttribute) SearchField(org.apache.ranger.common.SearchField) Collection(java.util.Collection) Metamodel(javax.persistence.metamodel.Metamodel) Map(java.util.Map) VXXTrxLog(org.apache.ranger.entity.view.VXXTrxLog)

Example 4 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project VaadinUtils by rlsutton1.

the class JpaBaseDao method findAllByAnyAttributes.

/**
 * Find multiple records by multiple attributes. Searches using OR.
 *
 * @param <SK>
 *            attribute
 * @param attributes
 *            AttributeHashMap of SingularAttributes and values
 * @param order
 *            SingularAttribute to order by
 * @return a list of matching entities
 */
public <SK> List<E> findAllByAnyAttributes(AttributesHashMap<E> attributes, SingularAttribute<E, SK> order) {
    final CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    final CriteriaQuery<E> criteria = builder.createQuery(entityClass);
    final Root<E> root = criteria.from(entityClass);
    criteria.select(root);
    Predicate where = builder.conjunction();
    for (Entry<SingularAttribute<E, Object>, Object> attr : attributes.entrySet()) {
        where = builder.or(where, builder.equal(root.get(attr.getKey()), attr.getValue()));
    }
    criteria.where(where);
    if (order != null) {
        criteria.orderBy(builder.asc(root.get(order)));
    }
    List<E> results = getEntityManager().createQuery(criteria).getResultList();
    return results;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Predicate(javax.persistence.criteria.Predicate)

Example 5 with SingularAttribute

use of javax.persistence.metamodel.SingularAttribute in project VaadinUtils by rlsutton1.

the class JpaDslAbstract method update.

/**
 * WARNING, order will not be honoured by this method
 *
 * @param attribute
 * @param value
 *
 * @return
 */
public <F extends Object> int update(Map<SingularAttribute<E, F>, F> updatemap) {
    Preconditions.checkArgument(orders.size() == 0, "Order is not supported for delete");
    CriteriaUpdate<E> updateCriteria = builder.createCriteriaUpdate(entityClass);
    root = updateCriteria.getRoot();
    if (predicate != null) {
        updateCriteria.where(predicate);
        for (Entry<SingularAttribute<E, F>, F> update : updatemap.entrySet()) {
            updateCriteria.set(update.getKey(), update.getValue());
        }
    }
    Query query = getEntityManager().createQuery(updateCriteria);
    if (limit != null) {
        query.setMaxResults(limit);
    }
    if (startPosition != null) {
        query.setFirstResult(startPosition);
    }
    int result = query.executeUpdate();
    getEntityManager().getEntityManagerFactory().getCache().evict(entityClass);
    return result;
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) TypedQuery(javax.persistence.TypedQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query)

Aggregations

SingularAttribute (javax.persistence.metamodel.SingularAttribute)30 Metamodel (javax.persistence.metamodel.Metamodel)13 Attribute (javax.persistence.metamodel.Attribute)11 ListAttribute (javax.persistence.metamodel.ListAttribute)8 MapAttribute (javax.persistence.metamodel.MapAttribute)6 Predicate (javax.persistence.criteria.Predicate)5 EntityType (javax.persistence.metamodel.EntityType)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 PluralAttribute (javax.persistence.metamodel.PluralAttribute)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 Path (javax.persistence.criteria.Path)3 Root (javax.persistence.criteria.Root)3 IdentifiableType (javax.persistence.metamodel.IdentifiableType)3 Type (javax.persistence.metamodel.Type)3 EntityManager (javax.persistence.EntityManager)2 Query (javax.persistence.Query)2 TypedQuery (javax.persistence.TypedQuery)2 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)2 Expression (javax.persistence.criteria.Expression)2