Johan's blog
Getting used to inheritance in JPA
In some cases, I am very eager to experiment with new technologies that are not even alpha-released. But in other cases, I am pretty conservative, sticking to the techniques I am used to.
With the Java EE 5 platform, I combined both attitudes. I started to use Java EE 5 immediately, especially the new query language, but it took me a while before I stopped using XML declaration and started to use annotations.
Now, years later, I started to explore another nice feature of the new JPA: inheritance. When I first used the J2EE platform, like many developers I had to get used to the idea that Entity Beans and inheritance are no good friends.
This conceptual incompatibility forced developers into a strange straddle: use Object Oriented technologies in the logic of the code, but don't use it in the persistence layer.
The EJB 3.0 spec supports inheritance. And actually, it is pretty easy.
My first experiment worked almost out-of-the box, without reading any manuals or tutorials (I copy-pasted and modified some colleague's code). The only problem I got was a runtime error, something likeCaused by: java.sql.SQLException: Unknown column 'DTYPE' in 'field list'
A quick google-search solved the problem in less than 1 minute: adding a field DTYPE on the superclass's table did the job.
There are a number of interesting resources on inheritance in JPA, including the following:
http://www.oracle.com/technology/products/ias/toplink/jpa/howto/use-inheritance.html which describes inheritance in Toplink, which is used in Glassfish, and http://java.sun.com/developer/technicalArticles/J2EE/jpa/#inpoly .
(disclaimer: I typically only start to read tutorials after I got something working).
Still, I didn't see many real-world use cases where JPA inheritance is used in the Java Enterprise platform. I guess a number of developers are like me, and are getting used and comfortable with the straddle: one leg in OO, one leg outside OO. I would encourage them to experiment with inheritance in the JPA, though. It is less painful.
comments:
