Directory

← Back

JPA Criteria Lazy Container

JPA 2.0 Criteria API extension to Lazy Query Container add-on

Author

Rating

Popularity

<100

This add-on uses JPA 2.0 type-safe queries written using the Criteria API, that automatically generate SQL for the programmer. The containers in this add-on automatically figure out the properties for the container, no manual step is required.

In simple situations that involve only one table, no query programming is needed, and the Vaadin Filterable and Sortable mechanisms are directly supported. The CriteriaContainer class supports this usage. It is also possible to program queries for grouping, sorting, filtering and any situation where more control is required. CriteriaContainer supports the addition of entities to the container.,

A second container allows retrieval of joined tables, and the creation of containers where a given item comes from any number of joined entities and computed values. The BeanTupleContainer supports this generalized access. Also, each of the entities returned is editable. When an entity appears in several tuples, it is the same entity -- changing the entity will be reflected everywhere when the item is written and the container refreshed. When using BeanTupleContainer, adding entities is done using the JPA API persist() function, and refreshing the container.

Nested properties are supported. For example, if the Person entity is joined to the Task entity via the assignedTo relationship, it will be possible to retrieve all the Person,Task pairs for which this relationship is true, and the item will allow retrieval of Person.name and Task.dueDate as item properties if these fields are present in the entities. The full Jakarta PropertyUtils syntax is supported, so you can also invoke getters on the retrieved objects.

Sample code

/** 
 * Define the query to be executed.
 * The container will add the restrictions from container filters, and apply the ordering
 * defined by the container.
 * 
 * @see org.vaadin.addons.criteriacore.AbstractCriteriaQueryDefinition#defineQuery(javax.persistence.criteria.CriteriaBuilder, javax.persistence.criteria.CriteriaQuery)
 */
@Override
protected Root<?> defineQuery(
        CriteriaBuilder cb,
        CriteriaQuery<?> cq) {

    // FROM task JOIN PERSON 
    Root<Person> person = (Root<Person>) cq.from(Person.class);
    task = person.join(Person_.tasks); 

    // SELECT task as Task, person as Person, ... 
    cq.multiselect(task,person);

    return task;  // if a join is present, it is safer to return a joined entity
}
/**
 * Define a new container based on a query definition.
 * Properties are automatically defined for all fields of the entities
 * returned by the query
 *
 * @return a new container
 */
@Override
protected BeanTupleContainer createTupleContainer() {
    cd = new CustomFilteringBeanTupleQueryDefinition(entityManager,true,100);
    BeanTupleContainer tupleContainer = new BeanTupleContainer(cd);
    return tupleContainer;
}

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

CriteriaContainer now supports nested retrieval container.addContainerProperty("assignee.class",...); table.setVisibleColumn("assignee.class"); The items of the container will then retrieve the value of assignee.getClass() when accessing the "assignee.class" property. Any string that can be understood by Jakarta PropertyUtils.getProperty can be used (e.g. person.address.zipcode or even person.children[0]).

CriteriaContainer now supports setKeyPropertyId(). The designated property will be used as item identifier. This enables getValue() to return a value from the entity (a foreign key) instead of the index in the container. This is especially useful with Select and similar components.

Released
2011-03-24
Maturity
BETA
License
Apache License 2.0

Compatibility

Framework
Vaadin 6.5+
Vaadin 6.0+ in 0.4.2
Vaadin 6.4+ in 0.1.0
Vaadin 6.6+ in 0.9.0
Browser
N/A

Vaadin Add-on Directory

Find open-source widgets, add-ons, themes, and integrations for your Vaadin application. Vaadin Add-on Directory
The channel for finding, promoting, and distributing Vaadin add-ons.
Online