2 ms·
I really feel like i'm owed a considerable number of nickels for all the times i've seen posts and comments about ORMs which miss the fact that they have powerf
by twic 8y ago
I really feel like i'm owed a considerable number of nickels for all the times i've seen posts and comments about ORMs which miss the fact that they have powerful query languages built in. JPQL is a genuinely pretty nice way of querying a database - it's basically SQL, plus object property syntax [1]. Here's a little example which concisely examines a many-to-many relationship:
SELECT DISTINCT p
FROM Player p
WHERE p.teams IS NOT EMPTY
Or navigation through a path (i made this example up, might not be quite right):
SELECT t.name, t.league.sport
FROM Team t
WHERE t.city = 'Ul Qoma'
If you're doing reporting where it doesn't make sense to pull out the entities, you can use a constructor expression [2] to materialise results as anything else you like (again, made up, might not be spot on):
// package hn; public class CountSummary { public CountSummary(String name, int count) {} }
SELECT NEW hn.CountSummary(l.name, COUNT(l.teams))
FROM League l
Certainly, there are more sophisticated features of SQL that are useful for analytical queries that aren't in this query language. That's why every ORM has an escape hatch where you can write raw SQL, but still use its machinery for turning results into objects.
[1] https://docs.oracle.com/javaee/6/tutorial/doc/bnbtl.html https://docs.oracle.com/javaee/6/tutorial/doc/bnbtl.html
[2] https://docs.oracle.com/javaee/6/tutorial/doc/bnbuf.html#bnbwc https://docs.oracle.com/javaee/6/tutorial/doc/bnbuf.html#bnb...