While preparing for a workshop on the EJB 3.0 Persistence API that I will present later today, I decided – against all the instructions in demo-school and presentation-college – to make a last minute change: replace the Glassfish libraries from build 20 (december 2005) with the new Glassfish stand-alone Persistence implementation (see: https://glassfish.dev.java.net/downloads/persistence/JavaPersistence.html) (that is 2 Mb instead of 80 or so). What I did not count on, is that since the build I used before, there have been several changes in the Annotations used for defining the Object Relational Mapping. As a result, my demos did not function anymore, and some of the labs were rendered incorrect.
Using the most recent EJB 3.0 Persistence Specification document I tried to correct, one by one, the errors my compilation as well as runtime execution ran into. Here a summary of the changes I had to make:
@ID – no longer has any attributes. No more generate and generator attributes
@GeneratedValue – the following method is now used for specifying the fact that the value for an attribute during insert into the database should be retrieved from a database sequence:
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ALS_PBR_SEQ") @Column public Long getId() { return id; }
We also need to import two classes:
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@NamedQuery – the attribute queryString is now simply called query.
@JoinTable – the attribute table (of type @Table) is dropped. Now we simply use name to specify the name of the JoinTable. For example:
@ManyToMany(cascade=CascadeType.ALL) @JoinTable(name = "als_authorships", joinColumns = { @JoinColumn(name = "atr_id") }, inverseJoinColumns = { @JoinColumn(name = "bok_id") })
Then the persistence.xml underwent some changes:
We need to add the attribute version=”1.0″ to the persistence element:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
And the property-names were changed; instead of just jdbc.driver, jdbc.url etc., the standalone Glassfish Persistence implementation requires the property names to be prefixed with toplink.jdbc…. It does not seem likely that this change is a JSR-220 modification, it is more likely that this reflects an internal change in the Glassfish code.
<properties> <property name="toplink.jdbc.driver" value="<database driver>"/> <property name="toplink.jdbc.url" value="<database url>"/> <property name="toplink.jdbc.user" value="<user>"/> <property name="toplink.jdbc.password" value="<password>"/> <property name="toplink.logging.level" value="INFO"/> </properties>
Since most of these changes seem improvements, I am not hugely disappointed. And I found about these just in time for my workshop.
Resources
http://trycatchfinally.blogspot.com/2006/01/experiments-with-ejb-30-id-annotation.html on how to get started with Glassfish Stand Alone persistence
Java Community Process on EJB 3.0 Persistence
Glassfish Reference Implementation of EJB 3.0 Persistence