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();
}
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);
}
}
}
};
}
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;
}
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;
}
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;
}
Aggregations