Recent changes in the EJB 3.0 Persistence specification (that invalidated my code)

Lucas Jellema
0 0
Read Time:2 Minute, 29 Second

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

 

About Post Author

Lucas Jellema

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director and Oracle Developer Champion. Solution architect and developer on diverse areas including SQL, JavaScript, Kubernetes & Docker, Machine Learning, Java, SOA and microservices, events in various shapes and forms and many other things. Author of the Oracle Press book Oracle SOA Suite 12c Handbook. Frequent presenter on user groups and community events and conferences such as JavaOne, Oracle Code, CodeOne, NLJUG JFall and Oracle OpenWorld.
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
Next Post

AJAX validation with Spring without changing any java-code

Related posts: Java 5 Annotations – Creating your own Annotation Type Some WebLogic Administration Essentialsbook reviews EJB an MDB best practices on WebLogic Cluster ADF DVT Speed Date: Interactive Bubble Graph JavaOne 2013: Java Mission Control
%d bloggers like this: