Search in sources :

Example 21 with SingularAttribute

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Predicate(javax.persistence.criteria.Predicate)

Example 22 with SingularAttribute

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());
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) Test(org.junit.Test)

Example 23 with SingularAttribute

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;
}
Also used : PersonInfo(common.advanced.PersonInfo) JPACriteriaQueryVisitor(org.apache.cxf.jaxrs.ext.search.jpa.JPACriteriaQueryVisitor) ArrayList(java.util.ArrayList) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Person(common.advanced.Person) Tuple(javax.persistence.Tuple)

Example 24 with SingularAttribute

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());
}
Also used : SingularAttributeJoin(org.hibernate.query.criteria.internal.path.SingularAttributeJoin) SingularAttribute(javax.persistence.metamodel.SingularAttribute) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) Type(javax.persistence.metamodel.Type) JoinType(javax.persistence.criteria.JoinType) PathSource(org.hibernate.query.criteria.internal.PathSource) Bindable(javax.persistence.metamodel.Bindable) Test(org.junit.Test)

Example 25 with SingularAttribute

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;
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) ConverterException(javax.faces.convert.ConverterException) FacesMessage(javax.faces.application.FacesMessage)

Aggregations

SingularAttribute (javax.persistence.metamodel.SingularAttribute)38 Attribute (javax.persistence.metamodel.Attribute)13 Metamodel (javax.persistence.metamodel.Metamodel)13 ListAttribute (javax.persistence.metamodel.ListAttribute)8 ArrayList (java.util.ArrayList)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)7 MapAttribute (javax.persistence.metamodel.MapAttribute)7 Test (org.junit.Test)7 Predicate (javax.persistence.criteria.Predicate)5 EntityType (javax.persistence.metamodel.EntityType)5 Root (javax.persistence.criteria.Root)4 PluralAttribute (javax.persistence.metamodel.PluralAttribute)4 EntityManager (javax.persistence.EntityManager)3 Path (javax.persistence.criteria.Path)3 IdentifiableType (javax.persistence.metamodel.IdentifiableType)3 Type (javax.persistence.metamodel.Type)3 VersionedPerson (org.datanucleus.samples.annotations.versioned.VersionedPerson)3 EntityTransaction (javax.persistence.EntityTransaction)2 Query (javax.persistence.Query)2 TypedQuery (javax.persistence.TypedQuery)2