Tuesday, 15 June 2010

Querydsl vs JPA 2.0 Criteria API: First-round knockout

DISCLAIMER: I am not affiliated with the Querydsl team in any way, shape or form.

I've been developing with JPA on and off for a while now, only on small projects, but enough to be comfortable with it. 


Like everyone else, I soon reached the point where I thought "ergh, I don't much like embedding strings in my code to query my objects". @NamedQueries get checked a little, but not enough. I've been hit with build errors from my Hudson build server that my IDE (Eclipse, of course) doesn't notice and found the approach little better than embedding SQL strings. I'd also worked a little with LINQ in .NET and liked what I saw. I'm holding my breath for lambda functions in JDK 7, but to be honest, I'm feeling a bit light-headed and my face has gone purple.

So I came to the Criteria API that comes with JPA 2.0. Hey! That looks good. It's not LINQ, bit it looks like it'll do what I want. Typesafe queries. IDE code-completion. Nice. That's going to make my life easier and better.

Can you see where we are going readers? Yes? Yes. That's right. Then I tried to use it. OK "select p from Pet p" is easy. But it takes 5 lines of code. I have to create a CriteriaBuilder. Then a CriteriaQuery. Then a Root. Then a TypedQuery. Only then can I get the results. 

Now, let's be clear. I can write SQL. I'be been writing SQL for 15 years. I know how to write SQL to get the results I need in the majority of situations (and believe me, I've been tempted to do just that). My first non-trivial CriteriaQuery with the JPA Criteria API was to create a report joining a handful of tables, selecting a couple of attributes from each of those tables. I can do this with SQL. It's simple. I can't do it with the Criteria API. I'm NOT saying it's impossible. I'm sure it is possible. I'm sure it's easy, when you know how. But I don't know how, and for the life of me I can't find straightforward documentation or examples that explain clearly how to do this. So I leave it and go and do something else. Then I come back to it. What is that code? What does it mean? What does it do? I thought this was supposed to be better?

I didn't want to veer away from the standard API. I like standards. I prefer to work with APIs that are widely used, that have a common experience base. It makes life easier. But I kept seeing Querydsl (http://source.mysema.com/display/querydsl/Querydsl) mentioned. I think I'd noticed it before when looking into LINQ equivalents for Java. But  thought it looked a little too green for my taste, too much vaporware potential. So I kept away. But out of frustration, I came back. Hey! They have current activity. They have frequent releases. They have documentation... with examples. They have snippets for my POM (subliminalUSE MAVEN). The code ...looks...readable! I can understand at a glance what its doing! This can't be right. It won't work when I try it. 

So I try it. Hang on. It works! I can replace 5 opaque lines with 3 transparent ones! Let's try my report query... Wait... It's easy! I can read the code! It makes sense! It produces the SQL I expect! 

Ok. Job done. Criteria API in the bin. Querydsl in my holster. No contest. 

If you are using JPA and want to write typesafe queries in an IDE, then stop using the JPA 2.0 Criteria API and start using Querydsl. Today. 

If you work on the Criteria API team, get someone who's never used it to try to figure it out for themselves. Then, take them out for a beer and say sorry to them. Then hire the Querydsl team. 

P.S. Querydsl team - change the name. It's rubbish ;-)

Tuesday, 25 May 2010

Using Hibernate 3.5.2 in JBoss 6.0.0-M3

DISCLAIMER: This works for me. I am not associated with JBoss/RedHat/Hibernate in any way, and this solution was arrived at via experimentation and an adequate working knowledge of Hibernate and JBoss. I'm happy to be corrected by anyone who knows better.

Envirnoment info:
Windows 7 x64 Ultimate
JDK 1.6.0_20 x64

In jboss-6.0.0.20100429-M3\common\lib, remove these jars:

hibernate-validator-legacy.jar
hibernate-validator.jar
hibernate-entitymanager.jar
hibernate-commons-annotations.jar
hibernate-jpa-2.0-api.jar
hibernate-annotations.jar
hibernate-core.jar


and replace with these:

geronimo-spec-jta-1.0.1B-rc4.jar
hibernate-validator-3.0.0.ga.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate-commons-annotations-3.2.0.Final.jar
hibernate-annotations-3.5.2-Final.jar
hibernate-core-3.5.2-Final.jar
hibernate-entitymanager-3.5.2-Final.jar


IIRC all these jars are from public Maven repositories. I don't recall having to add any manually to my master repo.

That's it. Like I say works for me. I intend to do the same thing on my Mac, which I assume will work the same. I'll post either way, should anyone be interested.

Tuesday, 30 March 2010

Alcohol tolerance

Yay. I'm becoming a cheap date. 1 x 330ml 5% lager + 1 x large 275(ish)ml glass of wine and I was feeling a bit pissed at the weekend. Now, you might say "lightweight", or perhaps "gayboy", I would say "bargain". Sur-weet. Just don't invite me on an all-dayer.

Methotrexate Diaries III - Folic frolics

And we're into the zone. Let me explain. Methotrexate doesn't start to take effect until at least 3 weeks into treatment.They actually say 3-12 weeks, but like anyone else desperate to experience some respite from the pain and discomfort of this shitty disease, I'm trying to keep optimistic and hope that I'll be one of the ones that sees an effect sooner. I guess that also means that the same window applies to side effects. Bah. My glass is half full, but some c**t has put a hole in the bottom.

This is the beginning of my 3rd week, so it's blood test time. Because I'm a Methotrexate newbie, I've got to come to the hospital for my bloods. That's a 50 minute drive, and I was wincing for the last few miles at the pain in my Achilles.

I thought I'd come early to minimise the impact on my day. It's bloody heaving ('scuse the pun), I've taken my ticket: number 63. In fairness, it's moved from 44 to 52 whilst I've typed this.

Oh, by the way - broadly, the point of folic acid is to reduce the likelihood of side-effects. I need to understand how so a little more, but that's the gist.

Beep beep. 63. That's me.

Tuesday, 23 March 2010

Methotrexate Diaries II

.....and we're into week 2.

More lovely little yellow pills down my neck. Why eight? Why not one big one? I imagine it's probably to allow finer dosage control and so they don't have to make different sized tablets, but still...

No side effects so far, apart from the crushing boredom of alcohol abstention. If, like me, you are an avid drinker, you may find the tools at http://www.drinkaware.co.uk/tips-and-tools/drink-diary/ useful. I spent some of my Saturday evening this weekend measuring out wine in a jug and doing unit calculations. Pathetic. It's enough to make a fellow cut it out altogether.

I also spent an unusually sober evening watching New Model Army. It was surprisingly tolerable; just not as enjoyable as when I've had a nice lovely leathering of booze.

It's supposed to be up to 3 months before I'm likely to notice a difference from this course of drugs. Last weeks my achilles was as painful as it's been in a while, so roll on 3 month's time. Jeez. It's not healthy to wish one's life away like this. Not healthy at all.



Wednesday, 17 March 2010

Methotrexate Diaries I

Methotrexate, Day 1

Well, it's actually Day 2 now. I started taking methotrexate yesterday, and thought about keeping a diary of the journey. Oh all right, it's not a journey, it's just a bunch of stuff that happened [H Simpson].

So, despite my conviction that I was seconds away from a fatal seizure all day, it never happened. Every twinge, every little ache, had me thinking "is this it?". And they say men can handle illness. Pshaw. I really need to wiki "fatal seizure" to make my feigning more convincing.

But, onwards for now. As I sais, it's Day 2, and it's a folic acid day. Now, on the one hand, I'm scared shitless of the side effects of methotrexate. So much so that I can't bring myself to read the rest of the leaflet and not sure that I ever will. More about that later. But on the other hand, I'm supposed to take folic acid on the days where I'm not taking methotrexate, and I know the consultant told me why and what for, but with the influx of information at the time, I for the life of me can't remember. So, I'm nicely parenthesised by my own ignorance. How refreshing.