JPA Criteria Lazy Container
JPA 2.0 Criteria API extension to Lazy Query Container add-on
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; }
Links
- Source code for sample app
- Source code for add-on
- Sample application WAR (with libs)
- Issue Tracker
- Release notes
- Javadoc
Compatibility
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
- Added setKeyPropertyId(). Instead of returning the index in the container, getValue will return the value of the indicated property. This makes it easy to use Select and similar components to retrieve the actual foreign key
- Fixed a bug related to refreshing tables after sorting. The "startingAt" index used internally for database retrieval was not always reset correctly.
- Released
- 2011-03-24
- Maturity
- BETA
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 6.0+
- Vaadin 6.5+ in 0.4.6
- Vaadin 6.4+ in 0.1.0
- Vaadin 6.6+ in 0.9.0
- Browser
- N/A