JavaOne report: EJB Persistence

Prior to going to JavaOne, the main subjects I was looking forward to were generics, EJB3 and the AOP panel. The actual experience was different though: sessions on generics did not show me much I had not already read online, and the main thing to take away from the AOP panel was that it can be useful, but don't overdo it - duh.

The EJB persistence session was interesting though: it looks like the EJB 3.0 spec will finally make Entity beans useful again. No more interfaces to implement, POJOs instead of abstract classes (so you can actually test against them outside of the container) and no more home+remote+bean+2deployment-descriptors per EJB.

Most importantly though, EJBQL becomes vastly more useful, allowing you to use it for almost anything you can do with SQL. You can, for instance, use bulk operations like

DELETE FROM Customer c 
WHERE c.status = 'inactive'

UPDATE Customer c 
SET c.status = 'outstanding' 
WHERE c.balance < 10000
Also, there will be support for sub-queries, as in
SELECT goodCustomer 
FROM Customer goodCustomer
WHERE goodCustomer.balance < (SELECT AVG(c.balance) FROM Customer c)

FROM Employee emp
             FROM Employee spouseEmp 
             WHERE spouseEmp = emp.spouse)
and joins will also be supported:
SELECT i.category 
FROM Item i JOIN j.bids b WHERE b.amount > :amount

FROM Item i LEFT JOIN FETCH i.bids WHERE i.category = 'paintings'
The FETCH above means that the bids objects will be prefetched from the database when the items are retrieved, instead of the current situation where there will be one extra query for every bid object.

EJBQL will even support GROUP BY and HAVING. My personal favorite though is projection, allowing you to retrieve select fields from select objects, as in

SELECT, c.status 
FROM Customer c JOIN c.orders o WHERE o.count > 100 (returns object array)

SELECT new CustomerDetails(, c.status, o.count) 
FROM Customer c JOIN c.orders o WHERE o.count > :ordercount
The first query will return an Object-array, the second will return actual CustomerDetails objects.

As interesting as all this was though, it still wasn't my favorite session, on which I'll blog more later this weekend...

TrackBack URL for this entry:

Wow, in other words, entity beans will mostly catch up to where Hibernate has been for the past year? How... nice, I guess. Everything mentioned above is already a feature of Hibernate. That and the fact that it already exists and works and has surprisingly good performance.

Don't get me wrong, I love session beans. But at this point entity beans are a lost cause as far as I'm concerned -- the problem has been solved, much better, elsewhere, and to me there's no advantage in switching back unless they somehow significantly surpass Hibernate in features and/or stability.

Posted by Steven Grimm at July 3, 2004 8:20 AM

Yes, as you can see, Gavin King was very involved in the new entity bean spec. The only O/R tool I have used personally is TopLink (with which I was not all that impressed), so I wasn't sure how much of this is a direct copy of hibernate, and how much is (slightly?) different.

I think that some advantages of having it be part of the spec though is that it will integrate easier into things like container managed transactions, and that it will become ubiquitous.

Posted by Luke Hutteman at July 3, 2004 8:34 AM
This discussion has been closed. If you wish to contact me about this post, you can do so by email.