Notes

Preface

1However, Pragmatic Programmer (1999) was already out there. After many years of being aware this book exists I’ve decided to buy it and read it. It is beyond any recommendation and tip 25 How to Balance Resources is tackling exactly this. Personally I’d say this book is more essential than Clean Code but both should be read in any case.

JPA Good Times, Bad Times

1My favourite is JPspec, section 4.8.5, mentioning that “use of DISTINCT with COUNT is not supported for arguments of embeddable types or map entry types”. In general, using embeddables may limit our options, which is a pity and may force design decisions not to use them where we otherwise would.

2One especially big pain is no support for date arithmetic like GETDATE() + 1.

3For paginating with fetch across to-many relations see the chapter Avoid N+1 select, concretely this section.

4You can see this demonstrated in examples/querydsl-basic if you run: mvn test-compile exec:java -Dexec.mainClass="tests.GeneratedIdSettingDemo"

5Run the code from this GitHub example and see the console output.

6We will, however, prefer more expressive Querydsl that generates JPQL.

7See [JPspec], section 3.10.8.

8In many popular databases that is. Standard SQL pagination is quite young and I doubt database vendors agreed to implement it.

9We can check video with this and more JPA Gotchas. Note that many are related to the use of Hibernate and not necessarily general for other JPA providers.

10You can try it yourself when you watch the console output while debugging and stepping through the class tests.PersistAndSetDemo from querydsl-basic module.

11Second-level cache is most popular term, used also in [JPspec]. It appears in [PJPA2] too, but in-memory cache is used more often there.

12Tested with Hibernate and EclipseLink.

13Books on Java performance often mention that memory versus CPU is not such a clear cut in JVM, as memory may affect garbage collector in such a way that we actually trade CPU for memory and CPU. Another reason to be really aware of what we do – we should not rely on automagic and we should always measure the impact as well.

14They may think programmers learn themselves, many of them do. Sometimes I think though, that the managers expecting self-learning most are those who don’t learn anything themselves.

15In a different post, domain objects were “enriched” with all kinds of serialization needed, effectively binding possibly all domain objects to possibly all technologies you need. I believe OOP has been there already, maybe not before I was born, but definitely long before I started programming. Author later used various decorators to keep classes focused and small, but this added complexity (software is all about trade-offs).

16While class diagram does not say exactly the same like E-R diagram I used it successfully to communicate design of tables to my DBA who had absolutely no problem to understand it, even for cases where many-to-many associative table was implied by a named line. E-R diagram was much easier to generate ex-post for documentation purposes.

Opinionated JPA

1This was late March 2016. What I didn’t check is the newest Hibernate version then. Hibernate 5.1 release notes from Feb 10, 2016 brought the news about Entity joins (or ad hoc joins) (the first item). I would find out four months later. Of course it still is not JPA 2.1 compliant, but with two major ORMs backing this idea it becomes definitely much more practical.

2Hibernate sometimes does it out of the box and sometimes does not do it even when the relationship is marked with its proprietary annotation as @Fetch(FetchMode.JOIN). EclipseLink does not do it by default, but can be persuaded with its own annotation @JoinFetch(JoinFetchType.OUTER).

3View from the perspective of a service layer, it could be model in MVC at the presentation layer. We can also talk about client of the service layer but client may be just as confusing out of context.

4Example source code: https://github.com/virgo47/opinionatedjpawithquerydsl/blob/master/manuscript/examples/querydsl-basic/src/test/java/tests/Pagination.java

5There is also Hibernate specific fetchAll() that loads lazy single-valued properties, but this is beyond JPA specification.

6http://www.querydsl.com/static/querydsl/4.1.4/reference/html_single/#result_handling

7See [HPJP], introduction of the chapter Fetching in part JPA and Hibernate.

8The whole Java EE 8 is kind of dormant as of 2016.

9This chapter is based on my blog post: https://virgo47.wordpress.com/2015/05/05/jpa-modularity-denied

10Trends in software are not necessarily always progressive, we all know plenty of useless buzzwords and sometimes what is en vogue are not the best ideas available.

11Amazon reviews of this book mostly settle on 4 stars but there are many lower scores for various reasons. Some say it doesn’t cover OSGi enough, some that it covers it too much. I think it covers it just fine – not too much, mostly for examples, as it focuses on the principles of modularity in the first place.

Common problems

1If I see a demand for the second edition of this book I definitely plan to elaborate on an example of such a framework.

2Perhaps default methods in interfaces could help with some of it, but these came with Java 8 and it’s unlikely to affect existing versions of Querydsl.

Final thoughts

1Available in Hibernate starting with version 5.1.0, released early in 2016.

2Querydsl project seems more sleepy in 2017 but it still provides and there is a lot of interest in it (e.g. Atlassian is using it a lot and they wish to get involved).