Thoughts about architecture
Most people slice their cakes like this:
Image by Elaine Ashton
and in most cases not horizontally but still many developers, like i did in the past, slice their packages that way.
Really, just slapping classes into packages named “Model”, “View” and “Controller” doesn’t implement that pattern. @jensschauder has written much more eloquent than i about this here (Jens does also a pretty good talk about that topic and has some really nice drawings in it as well, if you should have the opportunity, check it out) and @olivergierke has taken it further here. Take your time, read both posts. Its worth it. Rethinking my own organization of code the last years helped me writing better code. Looking at a recent project that isn’t divided into libraries yet, i could easily rip those modules apart and deploy them separately.
So, why am i writing this, when so much has already been written by smarter people than i?
The last weeks i’ve been working on a new frontend for an old but really not outdated project that handles a lot of time series, prices and tenders, all in a multi tenant fashion. The database model is really complex but not complicated and is there to stay. The current codebase does many things right in the database (stored PL/SQL procedures) and that won’t change much.
In the course of the last 5 to 8 years I’ve learned so much about Hibernate and got to love the stuff that the Spring Data team is doing on Spring Data JPA and i can say that i really come far, using HQL or now JPQL and also the criteria query api (together with the static meta model) that nobody else seem to like. For example, using semi-joins to determine if stuff is active or not:
public static Specification<Projekt> isAktiv() { return (root, query, b) -> { final Subquery<String> outerSubquery = query.subquery(String.class); final Root<Projektversion> outerSubqueryRoot = outerSubquery.from(Projektversion.class); final Join<Projektversion, LieferangebotEntity> joinLieferangebot = outerSubqueryRoot.join(lieferangebot); final Subquery<String> innerSubquery = query.subquery(String.class); final Root<Projektversion> innerSubqueryRoot = innerSubquery.from(Projektversion.class); return b.exists( outerSubquery .select(b.literal("")) .where(b.and( b.equal(outerSubqueryRoot.get(projekt), root), b.isNotNull(joinLieferangebot.get(vertragsabschlussdatum))), b.not(b.exists( innerSubquery .select(b.literal("")) .where(b.and( b.equal(innerSubqueryRoot.get(projekt), outerSubqueryRoot.get(projekt)), b.greaterThan(innerSubqueryRoot.get(versionsdatum), outerSubqueryRoot.get(versionsdatum)) )) )) ) ); }; } |
Using this with Spring Data Repositories and specification makes JPA actually fun. But with jOOQ i can write it down like i would in SQL:
private static final Condition IS_ACTIVE_CONDITION = exists( select(val("")) .from(outer) .join(LIEFERANGEBOTE) .on(LIEFERANGEBOTE.LIEFERANGEBOTS_ID.eq(outer.LIEFERANGEBOTS_ID)) .where( outer.PROJEKT_ID.eq(p.ID) .and(LIEFERANGEBOTE.ABSCHLUSSDATUM.isNotNull()) .and(not(exists( select(val("")) .from(inner) .where( inner.PROJEKT_ID.eq(outer.PROJEKT_ID) .and(inner.VERSIONSDATUM.gt(outer.VERSIONSDATUM)) ) ))) ) ); |
Lukas promotes jOOQ for that kind of usage very well. But i say, it’s the simple things. I cannot only write elaborate queries, I can also write down what i want to select. jOOQ doesn’t try to be a mapping from a relational world into the slices of my application, it just maps tables and their columns 1:1. It’s up to me what i select. I could achieve this with native or JPQL queries as shown here, but than what’s the point of mapping all those entities anyway (to be honest, i’ve already done this on several occasions)? Another solution would be the new entity graphs but than i end up with either half-empty entities or getting nice surprises from lazy loaded associations.
What every JPA solution has in common, at least to me, is the fact that one ends up with one big package containing pretty much every entity there is. To create complete mappings from the relational into the object oriented world those entities depend on each other and i cannot slice it the way the application works.
You might say that this persistence package is analogue to the generated jOOQ tables and records, but i think they are quite different. The generated tables and records are mere means of accessing the same thing in the database, not more but also not less. Building a full fledged mapping is a lot more and implicates a lot more.
Not being forced to put effort into defining a logical and accurate class hierarchy to work with JPA but instead modeling and filling DTOs and business objects right from the database in a way that fits architectural slices of an application gives back a lot of freedom for writing cleaner and easier to understand applications.