use of javax.persistence.metamodel.SingularAttribute in project VaadinUtils by rlsutton1.
the class JpaBaseDao method findAllByAttributes.
/**
* Find multiple records by multiple attributes. Searches using AND.
*
* @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> findAllByAttributes(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.and(where, builder.equal(root.get(attr.getKey()), attr.getValue()));
}
criteria.where(where);
if (order != null) {
criteria.orderBy(builder.asc(root.get(order)));
}
TypedQuery<E> query = getEntityManager().createQuery(criteria);
JpaSettings.setQueryHints(query);
return query.getResultList();
}
use of javax.persistence.metamodel.SingularAttribute in project hibernate-orm by hibernate.
the class MetadataTest method testLogicalManyToOne.
@Test
public void testLogicalManyToOne() throws Exception {
final EntityType<JoinedManyToOneOwner> entityType = entityManagerFactory().getMetamodel().entity(JoinedManyToOneOwner.class);
final SingularAttribute attr = entityType.getDeclaredSingularAttribute("house");
assertEquals(Attribute.PersistentAttributeType.MANY_TO_ONE, attr.getPersistentAttributeType());
assertEquals(House.class, attr.getBindableJavaType());
final EntityType<House> houseType = entityManagerFactory().getMetamodel().entity(House.class);
assertEquals(houseType.getBindableJavaType(), attr.getBindableJavaType());
}
use of javax.persistence.metamodel.SingularAttribute in project tesb-rt-se by Talend.
the class PersonInfoStorage method getTypedQueryTuple.
public List<PersonInfo> getTypedQueryTuple(SearchContext context, String expression) {
// Get search condition encapsulating the query expression
SearchCondition<Person> filter = getSearchCondition(context, expression);
// Initialise JPA2 visitor which can convert the captured search expression
// into JPA2 TypedQuery
JPACriteriaQueryVisitor<Person, Tuple> jpa = new JPACriteriaQueryVisitor<Person, Tuple>(em, Person.class, Tuple.class);
// Convert
filter.accept(jpa);
// Shape the response data with selections and Tuple
List<SingularAttribute<Person, ?>> selections = new ArrayList<SingularAttribute<Person, ?>>();
selections.add(Person_.id);
jpa.selectTuple(selections);
// Get CriteriaQuery and create TypedQuery
CriteriaQuery<Tuple> cquery = jpa.getQuery();
TypedQuery<Tuple> typedQuery = em.createQuery(cquery);
// Run the query
List<Tuple> tuples = typedQuery.getResultList();
// Return the results
List<PersonInfo> infos = new ArrayList<PersonInfo>(tuples.size());
for (Tuple tuple : tuples) {
infos.add(new PersonInfo(tuple.get(Person_.id.getName(), Long.class)));
}
return infos;
}
use of javax.persistence.metamodel.SingularAttribute in project hibernate-orm by hibernate.
the class SingularAttributeJoinTest method testEntityModeMapJoins.
/**
* When building a join from a non-class based entity (EntityMode.MAP), make sure you get the Bindable from
* the SingularAttribute as the join model. If you don't, you'll get the first non-classed based entity
* you added to your configuration. Regression for HHH-9142.
*/
@Test
public void testEntityModeMapJoins() throws Exception {
CriteriaBuilderImpl criteriaBuilder = mock(CriteriaBuilderImpl.class);
PathSource pathSource = mock(PathSource.class);
SingularAttribute joinAttribute = mock(SingularAttribute.class);
when(joinAttribute.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.MANY_TO_ONE);
Type joinType = mock(Type.class, withSettings().extraInterfaces(Bindable.class));
when(joinAttribute.getType()).thenReturn(joinType);
SingularAttributeJoin join = new SingularAttributeJoin(criteriaBuilder, null, pathSource, joinAttribute, JoinType.LEFT);
assertEquals(joinType, join.getModel());
}
use of javax.persistence.metamodel.SingularAttribute in project microservices by pwillhan.
the class PersistentAttributeConverter method getAttribute.
public SingularAttribute getAttribute(String value) {
// TODO: This only supports 'Item_.name', not 'User_.address.city.zipCode'
String entityName = value.substring(0, value.lastIndexOf("."));
String attributeName = value.substring(entityName.length() + 1);
SingularAttribute attribute = null;
for (EntityType<?> entityType : emf.getMetamodel().getEntities()) {
if (entityType.getName().equals(entityName)) {
try {
attribute = entityType.getSingularAttribute(attributeName);
} catch (IllegalArgumentException ex) {
throw new ConverterException(new FacesMessage("Persistent entity '" + entityName + "' does not have attribute: " + attributeName));
}
}
}
if (attribute == null)
throw new ConverterException(new FacesMessage("Persistent attribute not found: " + value));
return attribute;
}
Aggregations