-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
52 lines (34 loc) · 1.32 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
What is EQ4J?
EQ4J - Entity Query for JPA - is a type safe, entity object based query API on top of JPA-QL.
Why EQ4J?
The problem with JPA-QL is it is String based, so the Java type system can not help here.
If you have typos on field name, you wouldn't find out until you actually run it.
If you want to change name, refactor won't be able to help, you have to find & replace.
Yes, there is Criteria API, but it sucks. it is ugly, cumbersome, hard to use and maintain.
How EQ4J works?
EQ4J is built around the JPA Entity, the query is written in strong typed Java codes and eventually
get translated into String based JPA-QL.
for example, given an entity class:
@Entity
public class User {
private int age;
private String name;
// more fields
public int getAge() {
return age;
}
public String getName() {
return name;
}
// more codes
}
JPA-QL:
select u from User u where u.name = :name and u.age > :age
EQ4J:
Select<User> select = select();
User u = select.from(User.class);
select.where(u.getName(), equalsTo("some name"))
.and(u.getAge(), greatThan(12));
// convert to javax.persistence.TypedQuery
// can either get result or do a bit more settings
TypedQuery<User> query = select.toTypedQuery(entityManager);