|
| 1 | +[[dynamic-queries]] |
| 2 | +== Dynamic Queries |
| 3 | +:root-project-dir: ../../../../../../../.. |
| 4 | + |
| 5 | +Hibernate offers an API for creating dynamic representations of a query, augmenting that representation and then creating an executable form of that query. The idea is similar in concept to <<chapters/query/criteria/Criteria.adoc#criteria,criteria queries>>, but focused on ease-of-use and less verbosity. |
| 6 | + |
| 7 | +[NOTE] |
| 8 | +==== |
| 9 | +These APIs are new in 7.0 and considered incubating. |
| 10 | +==== |
| 11 | + |
| 12 | +There is support for both <<dynamic-selection,selection>> and <<dynamic-mutation,mutation>> queries via the `DynamicSelection` and `DynamicMutation` contracts, respectively. These can be obtained from both `Session` and `StatelessSession`. |
| 13 | + |
| 14 | +[[dynamic-selection]] |
| 15 | +=== DynamicSelection |
| 16 | + |
| 17 | +A `DynamicSelection` allows to iteratively build a query from a "base", adjust the query by adding sorting and restrictions and finally creating an executable <<hql-SelectionQuery,`SelectionQuery`>>. |
| 18 | + |
| 19 | +[NOTE] |
| 20 | +==== |
| 21 | +For 7.0, only HQL is offered as a base. |
| 22 | +==== |
| 23 | + |
| 24 | +==== |
| 25 | +[source, java, indent=0] |
| 26 | +---- |
| 27 | +Session session = ...; |
| 28 | +// create using the HQL "from Book" as the base |
| 29 | +DynamicSelection<Book> ds = session.createDynamicSelection( |
| 30 | + "from Book", |
| 31 | + Book.class |
| 32 | +); |
| 33 | +// from here we can augment the base query "from Book", |
| 34 | +// with either restrictions |
| 35 | +ds.restriction( |
| 36 | + Restriction.restrict( |
| 37 | + Book_.suggestedCost, |
| 38 | + Range.closed(10.00, 19.99) |
| 39 | + ) |
| 40 | +); |
| 41 | +
|
| 42 | +// or here with some sorting |
| 43 | +ds.order( |
| 44 | + Order.asc(Book_.suggestedCost) |
| 45 | +) |
| 46 | +---- |
| 47 | +==== |
| 48 | + |
| 49 | +[NOTE] |
| 50 | +==== |
| 51 | +Notice that generally the JPA static metamodel is a convenient and type-safe way to help build these sorting and restriction references. |
| 52 | +==== |
| 53 | + |
| 54 | +After adjusting the query, we can obtain the executable `SelectionQuery`: |
| 55 | + |
| 56 | +==== |
| 57 | +[source, java, indent=0] |
| 58 | +---- |
| 59 | +SelectionQuery<Book> qry = ds.createQuery(); |
| 60 | +List<Book> books = qry.getResultList(); |
| 61 | +---- |
| 62 | +==== |
| 63 | + |
| 64 | +These calls can be chained, e.g. |
| 65 | + |
| 66 | +==== |
| 67 | +[source, java, indent=0] |
| 68 | +---- |
| 69 | +SelectionQuery<Book> qry = session.createDynamicSelection( |
| 70 | + "from Book", |
| 71 | + Book.class |
| 72 | +).restriction( |
| 73 | + Restriction.restrict( |
| 74 | + Book_.suggestedCost, |
| 75 | + Range.closed(10.00, 19.99) |
| 76 | + ) |
| 77 | +).order( |
| 78 | + Order.asc(Book_.suggestedCost) |
| 79 | +) |
| 80 | +.createQuery(); |
| 81 | +---- |
| 82 | +==== |
| 83 | + |
| 84 | +[NOTE] |
| 85 | +==== |
| 86 | +We expect, in future releases, to add the ability to handle pagination. |
| 87 | +
|
| 88 | +We also expect to add the ability to use <<criteria,Criteria>> references as the base. Possibly even `TypedQueryReference` references. |
| 89 | +
|
| 90 | +// todo : consider these forms as well: |
| 91 | +// - session.createDynamicSelection(Book.class) |
| 92 | +// - session.createDynamicSelectionForDynamicModel(String rootEntityName) |
| 93 | +==== |
| 94 | + |
| 95 | +[[dynamic-mutation]] |
| 96 | +=== DynamicMutation |
| 97 | +// todo : ... |
| 98 | +TODO |
0 commit comments