Tales from the jar side: Spring Data JPA, NFJS is back, The temptation to go meta, Another chess adventure, and good tweets
Tweet I saw on Rosh Hashanah: "I'm going to stay up late to watch the matzoh ball drop."
Welcome, jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of September 5 - 12, 2021. This week I taught a new course on Spring Data JPA on the O’Reilly Learning Platform and an NFJS Virtual Workshop on Reactive Spring. I also played in another of those online one-day chess tournaments, which was a bit too dramatic for my liking. A bit more on that later.
If you’re not one already, please consider becoming a jarhead by subscribing to Tales from the jar side using this link:
No Fluff, Just Stuff, Live
I also received confirmation that the first live NFJS event since the start of the pandemic will actually proceed, in Boston (more specifically, Wakefield, MA) the weekend of September 24. I’m scheduled to give nine (!) talks as well as the keynote Friday evening, so ten in all. From the conference speaker page:
I expect to have some ebook copies of Help Your Boss Help You available to raffle off at the keynote, so if you’re planning to attend, you might want to stay for that.
Regarding all those multi-part sessions, the testing workshop part one will be mostly about JUnit 5, and part two will be mostly on Mockito. For the Spring MVC talks, part one will involve basic restful web services, and part two will incorporate Spring Data and testing. The Latest Features in Java talks will cover all the major topics from Java 8 through Java 17, which is due to be released this coming Tuesday, so fortunately I’ll have a few dates to do the updates after the release happens.
Since the event is taking place less than two weeks from now, maybe I should start preparing my slides. :)
Just kidding. First of all, most of my talks are based on existing training courses, so it’s largely a case of narrowing down the highlights and the best examples. Second and arguably more important, even if it were true that I needed to prepare at the last minute, I would never, ever say so out loud in any forum where one of the attendees might hear me. I feel a statement like that implies I’m not taking the audience seriously, and comes across as disrespectful. Why should the attendees care if I clearly don’t?
I fell into that trap early in my training career. My first year teaching, I was using third-party materials that had some serious deficiencies. During the class, I couldn’t help pointing out the sections that were out of date or didn’t reflect the current best practices. Another member of my company was sitting in, and during a break he took me aside.
“You can’t do that,” he said. “The students paid for this class — or at least their companies did. They have to believe they’re getting the best training available. You can tell them about alternatives, but you can’t trash the materials in front of them. If the materials so bad, the students will wonder why we’re using them in the first place. We can re-evaluate them before our next class, but for this one, they’re the best we could provide.”
He was right, of course. We used those materials for reasons having to do with cost and availability, plus the fact that I didn’t have time to build my own. But that shouldn’t affect the student experience. Even more, my criticism of the materials was an attempt to make myself look good by blaming any problems on the materials. As the saying goes, it’s a poor craftsman that blames his tools when difficulties arise.
That brings up another lesson I had to learn over time, which is: when you’re giving a class the first time and not sure about what you’re doing, there’s an enormous temptation to go meta and make excuses. On a related note, I taught that Spring Data course for this first time this week.
Spring Data JPA and Going Meta
I mentioned last week that I had a course scheduled for Spring Data, and that I was sad I couldn’t use the Grails framework. I’ve been dealing with Hibernate (the most common implementation of the Java Persistence API specification, used by Spring Data JPA by default) for over a decade now, but a lot of my actual understanding comes from its behavior inside Grails. While Spring has evolved fairly substantially in the last few years and I’ve been good about keeping up with it, Hibernate has been much slower to change, and the demand for related training courses has also been lower.
Or so I thought when I started preparing, mostly because the Hibernate User Guide still looks much like it did back then. What I didn’t realize was that the Spring Data JPA reference documentation shows that some of the things I was used to doing in Hibernate no longer work the same way now.
For example, one of the ways to optimize efficiency in Hibernate is to make lazy queries eager. According to Hibernate, all associations are fetched lazily. If you ask for a Department, you don’t retrieve the list of associated Employees in that department unless you deliberately fetch them as well. If all associations were eager, the thinking goes, then a single query could wind up fetching half the data in the database. By making them all lazy, you may have to do additional queries, but at least each one will be as small as possible.
But wait, that’s not true any more. Here’s a highlighted snippet from the Hibernate reference manual:
Whoa. I knew that JPA did eager fetching for one-to-one associations, but many-to-one as well? That’s different (or I missed it before), and the Hibernate people disagree with it so strongly they felt the need to add their own disclaimer to the documentation. Passive aggression FTW, I suppose.
I definitely talked about that in class, but I during my preparation I had another surprise waiting. The Hibernate docs in that section show an Employee class with the following relationship to Department:
In my analogous example, I changed FetchType.LAZY to FetchType.EAGER and expected to see an eager fetch, as you might expect, but no such luck. As it turns out, there are two ways to make the fetch eager. One is to use JPQL (Java Persistence Query Language) with a LEFT JOIN FETCH statement, as in this example between Employee and Project:
I knew about that, but I still thought the annotation way would work. But it turns out there’s a new way, which I didn’t know about. It’s called JPA Entity Graphs. In my example, that looks like:
When I fetch the Knights, the query should also get their quests and then all the tasks that are part of each quest. The result is a SELECT FROM Knight with a LEFT OUTER JOIN for both Quest and Task.
That’s quite a mouthful, but it does what was requested.
I found a few more items that were new to me as well. You can now do what they call closed projections to select some columns from a table and return them as an interface type (!), where Hibernate will generate an implementation class for you. Also, criteria queries have changed, and are now called Specifications in the Spring Data documentation. I also was able to do a stored procedures example, even though the resulting blur of annotations is way too verbose for my liking. Auditing has changed, too. It’s all based on annotations now, which have to appear in the relevant entity classes and must be enabled in a configuration class.
What does all this have to do with my comment about going meta? Since so many of my examples were new, and this was my first time teaching this course, I had to physically stop myself from telling that to the students. I had to act like everything was planned, and that each demo worked exactly as I expected, and if anything was missing it was by choice.
That’s not to say be dishonest. The basic principle is, don’t go looking for trouble, and don’t start off the whole class by making excuses. If something goes wrong, fine, talk about that specific issue, but don’t anticipate problems by saying I’ve never taught this before or I’m not sure whether this is going to work or any weasel words like that. It’s okay to admit I don’t know something (though I have a theory on that, too, but I’ll save it for another time), but don’t undermine the students’ confidence in the class before the first demo.
As it happened, the one important problem I had was one I knew was going to happen: I had too many topics to cover and had trouble managing the schedule. That’s inevitable the first time, because you don’t know what questions are likely to come up, you don’t know how well certain explanations are going to work, and you don’t know how long the actual demos are going to take. Even if you practice, everything is different in front of a live audience. Fortunately, timing issues fix themselves with experience. By the third or fourth time I give the class, it should be routine. At least the O’Reilly Learning Platform is a subscription service, so students can repeat any class they like at no cost.
As a final note, the real expert I respect on these topics is Thorben Janssen. I’ve paid myself for his online course on Spring Data JPA, and on his site he has plenty of free cheat sheets, articles, and YouTube videos on how to make all this stuff work. I must admit I don’t necessarily agree with all his recommendations, but when we disagree I assume I’m probably wrong until proven otherwise.
Chess Is An Evil, Evil Game
O Chess, I will be avenged on thee for the loss of my time! … It hath not done with me when I have done with it.
— letter from a minister to his friend, 1680
Yesterday I played in the online CCA September Open, in the under 1900 section. That’s the same monthly tournament I played in July and August, and each time it’s an event. This time my results were:
Lost to a good player.
Beat a good player when I should have lost, but he made one too many mistakes.
Fell into an opening trap and was almost immediately down a piece to a weaker player. Hung around long enough for him to make a mistake which should have led to a draw, but he blundered and I won. He will not be happy about this game.
Lost to a good player when I got clever but he saw something I missed.
Ugh. Nightmare. Had a completely winning position against someone rated way below me. Managed to botch it completely into a draw by perpetual check, but I couldn’t accept that, so I managed to throw it away completely and lost. It’s a good thing the game was online, because otherwise I might still be looking for the pieces I hurled across the room. Partly karma, I guess, for my earlier win.
The No Fluff, Just Stuff season is starting again soon (see above), so I won’t have too many opportunities to play for a while. That’s just as well. It’s going to take me a while to get over that one.
Miscellaneous
My full podcast/screencast with Trish Gee for the GOTO people came out this week.
I really enjoyed talking to Trisha. Here’s a deep philosophical question to ponder:
Why, indeed? Speaking of Dad jokes:
I hate when that happens.
Evolution of product development:
(MVP stands for Minimum Viable Product.)
And finally, this tweet thread on the historical relationship between ham and cheese and hard-backed books is quite a ride:
For jarheads only
Jarheads can get the ebook versions of Help Your Boss Help You at the Pragmatic Programmers using the coupon code 7bc968c446 at checkout for a 35% discount. The code is good until the end of September, 2021.
If you’re not a subscriber, that’s fine. You get to use the coupon too.
As a reminder, you can see all my upcoming training courses on the O’Reilly Learning Platform here and all the upcoming NFJS Virtual Workshops here.
Last week:
Spring Data Fundamentals, on the O’Reilly Learning Platform
Reactive Spring, an NFJS Virtual Workshop
This week:
Managing Your Manager, on the O’Reilly Learning Platform. I hope to release the audiobook version by then.
Recording an updated video for my Spring and Spring Boot course on the O’Reilly Learning Platform. That should be fun.