|
2 | 2 |
|
3 | 3 | Create JPQL queries through matching Entity classes in the "hamcrest way". |
4 | 4 |
|
5 | | -The matcher user can extend the functionality with its own statements (created in similar fashion as in the Statements class). |
6 | | - |
7 | 5 | By default it translates the queries into JPQL. Native SQL support can be enabled by calling the #nativeQuery(true) method. |
8 | 6 |
|
9 | 7 | TODOs : |
10 | 8 |
|
11 | 9 | * Allow nesting classes |
| 10 | +* Having, group by, order by |
| 11 | + |
| 12 | +About... |
| 13 | + |
| 14 | +1) Matchers |
| 15 | + |
| 16 | +Matches are the classes which we'll use to match our statements. |
| 17 | +To be able to use an Entity as a matcher, we need to create it using the EntityMatcher#matcher(Class<?> entity) method. |
| 18 | + |
| 19 | +``` |
| 20 | + final Person person = EntityMatcher.matcher(Person.class); |
| 21 | +``` |
| 22 | + |
| 23 | +Once created we can attach it to a builder, which will create the JPQL / SQL statements for us. |
| 24 | + |
| 25 | +``` |
| 26 | + final Builder<Person> builder = EntityMatcher.builder(person); |
| 27 | +``` |
| 28 | + |
| 29 | +We can use as many matchers as we want in our queries, so far we register them all while creating the builder. |
| 30 | + |
| 31 | +``` |
| 32 | + final Person person = EntityMatcher.matcher(Person.class); |
| 33 | + final Animal animal = EntityMatcher.matcher(Animal.class); |
| 34 | +
|
| 35 | + final Builder<Person> builder = EntityMatcher.builder(person, animal); |
| 36 | +``` |
| 37 | + |
| 38 | +2) Flexible return types. |
| 39 | + |
| 40 | +Before we saw that each Builder is associated to one type. Per design the first matcher listed in the #builder(...) method is considered the return type. That is convenient whenever we are interested in the Entity itself. |
| 41 | + |
| 42 | +For those cases that we want other return type we use the #select() method. For instance let's retrieve only the name of the Entity person. |
| 43 | + |
| 44 | +``` |
| 45 | + final EntityManager em = ...; |
| 46 | + final Person person = EntityMatcher.matcher(Person.class); |
| 47 | + final Builder<Person> builder = EntityMatcher.builder(person); |
| 48 | +
|
| 49 | + // SELECT person.name FROM Person person |
| 50 | + final List<String> names = builder.select(person.getName()).build(String.class).getMatching(em); |
| 51 | +``` |
| 52 | + |
| 53 | +We can also use Java Beans to store the retrived information. For instance assuming our Animal entity has a name and an age property, the following is valid. |
| 54 | +``` |
| 55 | + final EntityManager em = ...; |
| 56 | + final Person person = EntityMatcher.matcher(Person.class); |
| 57 | + final Builder<Person> builder = EntityMatcher.builder(person); |
| 58 | +
|
| 59 | + // SELECT person.name, person.age FROM Person person |
| 60 | + final List<Animal> names = builder.select(person.getName(), person.getAge()).build(Animal.class).getMatching(em); |
| 61 | +``` |
| 62 | + |
| 63 | +3) Statements |
| 64 | + |
| 65 | +There are two kind of statements. LhsStatements (left-hand-side) and LhsRhsStatements (left-hand-side and right-hand-side). |
| 66 | +A LhsStatements receives information about one capture (table / column), while a LhsRhsStament receive information about two captures. |
| 67 | +For instance { =, !=, like, >, >=, in, ... } are all LhsStatements since the statement composes of a table + column, an operation, plus maybe a value. |
| 68 | + |
| 69 | +As an example, let's find within a table associated to an entity Person, all people called "Xiermires". |
| 70 | + |
| 71 | +``` |
| 72 | + // Get a matcher for some entity. |
| 73 | + final EntityManager em = ...; |
| 74 | + final Person person = EntityMatcher.matcher(Person.class); |
| 75 | +
|
| 76 | + final List<Person> orders = EntityMatcher.builder(person) // |
| 77 | + .match(person.getName(), eq("Xiermires")) // |
| 78 | + .build().getMatching(em); |
| 79 | +``` |
| 80 | + |
| 81 | +<b>LhsStatements</b> allow AND and OR chains to simplify building process. |
| 82 | +``` |
| 83 | + final Person person = EntityMatcher.matcher(Person.class); |
| 84 | +
|
| 85 | + // This is allowed but a bit verbose |
| 86 | + EntityMatcher.builder(person) // |
| 87 | + .match(person.getName(), eq("Xiermires")) // |
| 88 | + .or(person.getName(), eq("Serimreix")); |
| 89 | +
|
| 90 | + // Same using an OR chain |
| 91 | + EntityMatcher.builder(person) // |
| 92 | + .match(person.getName(), eq("Xiermires").or(eq("Serimreix"))) |
| 93 | +``` |
| 94 | + |
| 95 | +Both these expressions translate into : SELECT person FROM Person person WHERE person.name = 'Xiermires' OR person.name = 'Serimreix' |
| 96 | + |
| 97 | +An additional example of the same principle would be to find all people older than 30, which ages are odd. |
| 98 | + |
| 99 | +``` |
| 100 | + // Same using an OR chain |
| 101 | + EntityMatcher.builder(person).nativeQuery(true) // |
| 102 | + .match(person.getAge(), gt(30).and(isOdd())) |
| 103 | +``` |
| 104 | + |
| 105 | +This would translate as : SELECT person FROM Person person WHERE person.age > 30 AND isOdd(person.age) |
| 106 | + |
| 107 | +isOdd is a store procedure which checks if the age is odd (captain obvious here). Since JPQL doesn't accept stored procedures, we need to switch to native mode (see nativeQuery(true)) |
| 108 | + |
| 109 | +<b>LhsRhsStatements</b> on the other hand receive two tables and two columns to build the statement with. A typical example would be a join. |
| 110 | + |
| 111 | +Let's for instance check which people share name with an animal (ehem... Indiana). |
| 112 | + |
| 113 | +``` |
| 114 | + final Person person = EntityMatcher.matcher(Person.class); |
| 115 | + final Animal animal = EntityMatcher.matcher(Animal.class); |
| 116 | + EntityMatcher.builder(person, animal) // |
| 117 | + .match(person.getName(), join(animal.getName())); |
| 118 | +``` |
| 119 | + |
| 120 | +Coding Statements is relatively straightforward. Both LhsStatement and LhsRhsStatement are instantiated with a Statement, where Statement is the following interface. |
| 121 | + |
| 122 | +``` |
| 123 | +
|
| 124 | +public interface Statement |
| 125 | +{ |
| 126 | + List<Part> toJpql(String lhsTableAlias, String lhsColumn, String rhsTableAlias, String rhsColumn, ParameterBinding params); |
| 127 | + |
| 128 | + // syntax sugar to create parts, toString(), etc. |
| 129 | + ... |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +For instance this creates a LIKE statement. |
| 134 | + |
| 135 | +``` |
| 136 | + public static <T> LhsStatement<T> like(T t) |
| 137 | + { |
| 138 | + return new LhsStatement<T>((lhsAlias, lhsColumn, rhsAlias, rhsColumn, params) -> Statement.create( |
| 139 | + tableColumn(lhsTable, lhsColumn), Connector.LIKE, valueOf(t))); |
| 140 | + } |
| 141 | +``` |
| 142 | + |
| 143 | +There are a couple of things to comment here. |
| 144 | + |
| 145 | +a) Why forcing some weird Part thing and not return a String directly ? |
| 146 | + |
| 147 | +```(lhsAlias, lhsColumn, rhsAlias, rhsColumn, params) -> tableColumn(lhsTable, lhsColumn) + " LIKE " + valueOf(t));``` |
| 148 | + |
| 149 | +Basically due to the NOT nature. Consider the following if we were using Strings. |
| 150 | + |
| 151 | +```...match(person.getName(), not(like("Xiermir%")));``` |
| 152 | + |
| 153 | +Here do this operations look like ?. |
| 154 | + |
| 155 | +``` |
| 156 | + enum Connector implements Negatable |
| 157 | + { |
| 158 | + LIKE(" LIKE ", " NOT LIKE "), EQ(" = ", " != "), GT(" > ", " < "), LT(" < ", " > "), IN(" IN ", " NOT IN "); |
| 159 | +
|
| 160 | + final String affirmed; |
| 161 | + final String negated; |
| 162 | +
|
| 163 | + boolean isNegated; |
| 164 | + String conn; |
| 165 | +
|
| 166 | + Connector(String affirmed, String negated) |
| 167 | + { |
| 168 | + this.affirmed = this.conn = affirmed; |
| 169 | + this.negated = negated; |
| 170 | + this.isNegated = false; |
| 171 | + } |
| 172 | +
|
| 173 | + @Override |
| 174 | + public void negate() |
| 175 | + { |
| 176 | + conn = isNegated ? affirmed : negated; |
| 177 | + isNegated = !isNegated |
| 178 | + } |
| 179 | +
|
| 180 | + @Override |
| 181 | + public String toString() |
| 182 | + { |
| 183 | + return conn; |
| 184 | + } |
| 185 | + } |
| 186 | +``` |
| 187 | + |
| 188 | +b) What is params ? |
| 189 | + |
| 190 | +The params interface allows to create parameters (?\d), which makes the statement creation easier in some cases. For instance, see the IN operation. |
| 191 | + |
| 192 | +``` |
| 193 | + public static <T> LhsStatement<T> in(Collection<T> ts) |
| 194 | + { |
| 195 | + return new LhsStatement<T>((lhsTable, lhsColumn, rhsTable, rhsColumn, params) -> Statement.create( |
| 196 | + tableColumn(lhsTable, lhsColumn), Connector.IN, params.bind(ts))); |
| 197 | + } |
| 198 | +``` |
| 199 | + |
| 200 | +This can also be used to bind parameters to stored procedures. |
| 201 | + |
| 202 | + |
| 203 | +4) Anything else ? |
| 204 | + |
| 205 | +Let's just mention functions. Others, like having, group by, etc. follow similar principles. |
| 206 | + |
| 207 | +As an example, let's get the max age of all persons. |
| 208 | + |
| 209 | +``` |
| 210 | + final EntityManager em = ...; |
| 211 | + final Person person = EntityMatcher.matcher(Person.class); |
| 212 | + final Builder<Person> builder = EntityMatcher.builder(person); |
| 213 | +
|
| 214 | + // SELECT max(person.age) FROM Person person |
| 215 | + final Integer age = builder.select(max(person.getAge())).build(Integer.class).getSingleMatching(em); |
| 216 | +``` |
12 | 217 |
|
13 | 218 | ------------- |
14 | | -Examples |
| 219 | +More Examples |
15 | 220 | ------------- |
16 | 221 |
|
17 | 222 | ```java |
@@ -90,6 +295,34 @@ Examples |
90 | 295 | final String bar = stringQuery.getSingleMatching(em); |
91 | 296 | } |
92 | 297 |
|
| 298 | + public void max() |
| 299 | + { |
| 300 | + final TestClass tc = EntityMatcher.matcher(TestClass.class); |
| 301 | + final Builder<TestClass> builder = EntityMatcher.builder(tc); |
| 302 | + |
| 303 | + final PreparedQuery<Integer> stringQuery = builder.select(max(tc.getFoo())).build(Integer.class); |
| 304 | + stringQuery.getSingleMatching(em); |
| 305 | + } |
| 306 | + |
| 307 | + public void distinct() |
| 308 | + { |
| 309 | + final TestOther to = EntityMatcher.matcher(TestOther.class); |
| 310 | + final Builder<TestOther> builder = EntityMatcher.builder(to); |
| 311 | + |
| 312 | + final PreparedQuery<Integer> stringQuery = builder.select(distinct(to.getBar())).build(Integer.class); |
| 313 | + stringQuery.getMatching(em); |
| 314 | + } |
| 315 | + |
| 316 | + public void maxDistinct() |
| 317 | + { |
| 318 | + final TestOther to = EntityMatcher.matcher(TestOther.class); |
| 319 | + final Builder<TestOther> builder = EntityMatcher.builder(to); |
| 320 | + |
| 321 | + // Hibernate JPQL complains about max(distinct(column)). Use native. |
| 322 | + final PreparedQuery<Integer> stringQuery = builder.select(max(distinct(to.getFoo()))).nativeQuery(true).build(Integer.class); |
| 323 | + stringQuery.getSingleMatching(em); |
| 324 | + } |
| 325 | + |
93 | 326 |
|
94 | 327 | @Entity |
95 | 328 | @Table(name = "TestClass") |
@@ -146,7 +379,7 @@ Examples |
146 | 379 |
|
147 | 380 | int foo; |
148 | 381 |
|
149 | | - @Column(name = "Bar_v2") |
| 382 | + @Column |
150 | 383 | String bar; |
151 | 384 |
|
152 | 385 | // test only |
|