EJB 3.0 Persistence is a topic that is high on our list. We have written a substantial number of blog articles on it after doing quite some research as well as roaming conferences and the internet for useful information and implementations as well as supporting tools. Now we know that the recent Oracle JDeveloper 10.1.3 release (freeware, not open source) has built in support for EJB 3.0 Persistence; for example a wizard Generate Entity Beans from Tables that creates our POJOs objects with all required EJB 3.0 Persistence Annotations. Oracle also has the lead in the Eclipse Dali plugin-project. Other parties working on Dali include JBoss (Hibernate) and SolarMetric (Kodo) – now BEA – and Versant.
The objective of Dali as found on the website http://www.eclipse.org/dali/ :"The goal of the
Dali EJB Object-Relational Mapping project is to
build extensible frameworks and exemplary tools for the
definition and editing of Object-Relational (O/R) mappings
for EJB 3.0 Entity Beans. EJB 3.0 O/R mapping support
will focus on minimizing the complexity of mapping by
providing creation and automated initial mapping wizards,
and programming assistance such as dynamic problem identification." One of the objectives for EJB 3.0 Persistence is ease of development which in part is supposed to happen through IDE support for the Annotations. That is exactly what Dali is trying to accomplish.
In this article, I will report on my first steps with Dali in Eclipse 3.1. I will discuss the installation as well as the functionality available at this time. Note that there is no production release – the 0.5 release is scheduled for very late in June 2006 (thanks Jan for pointing me to this schedule; don’t know why I missed it). To be frank, you can see where Dali is going, and it is doing some useful things already, but it is a little buggy at the same time. Now is a good time to get started, but it is too early for using it in anger.
By the way, the website also explains the name Dali: "The focus of Dali
is support for EJB 3.0 Object/Relational mapping which is
concerned with the persistence of in memory
Java POJOs to relational databases. The title of the famous
painting "The Persistence of Memory" aptly describes
the goal of the EJB 3.0 specification and provides the link
to the Spanish painter Salvador Dali."
Salvador DalÃ. The
Persistence of Memory. 1931.
Â© 2005 Salvador
DalÃ, Gala-Salvador DalÃ Foundation/Artists
Rights Society (ARS), New York
To download and install:
the WTP (Web Tools Project) â€“ get it using Eclipse Plugin Manager from http://download.eclipse.org/webtools/updates/
the Dali plugin – http://www.eclipse.org/dali/downloads.html - and extract the zip-archive to the
Eclipse home directory
sure that an EJB 3.0 Persistence API implementation is available â€“ for
example Glassfish RI, Hibernate 3.0 or Toplink.
the instruction in Starting with EJB 3.0 Persistence in Eclipse 3.1 using the GlassFish Reference Implementation ,
to create a new Java Project, with the GlassFish libraries included in the
to the "Persistence Perspective"–you don’t have to but this
perspective contains the views that support entity editing.
on a Java Project and choose Persistence -> Add Persistence.
window opens where you have to specify the database connection to use for
our Persistence Project.
If you do not have a database connection, you can navigate to the wizard
to create one:
the Persistence Properties view â€“ from the Window menu, pick Show View and
then Other. The next window is displayed; select Persistence Properties.
This opens a the Persistence Properties view that we will use to define
the Mapping details for our Entities.
that we have set up the Dali plugin â€“ the Persistence Perspective â€“ we can
start using it to help us create Mappings. Create a new class Publisher,
from File menu, New, Other â€“ Persistence, EJB 3.0 Entity:
Enter the package and the name of the Entity. It is created by the wizard:
select the Class. In the Persistence Properties view, you can now indicate
that this class is to be mapped as Entity, to the table ALS_PUBLISHERS.
The Dali plugin adds the @Entity and @Table annotations based on these
the properties Id, Name and Website in Class Publisher
the option Source, Generate Getters and Setters to create the get- and
set-methods for these properties.
select the getId method in the source editor or in the Persistence Outline.
We can now use the Dali Persistence Properties view to define the mapping
for this getter:
In this way, Dali is very helpful in creating the mapping annotations.
Note that we can still manually edit the annotations as well.
What Dali currently does not do, is create a class and its properties
based on a table â€“ something that JDeveloper can do for us.
Dali really shines in my opinion is with more complex mappings: properties
that represent relationships between entities. Suppose we create a second Entity called
Book for our ALS_BOOKS table. This entity has a number of attributes, like
id, title, publishYear etc.:
- Now we
know from our database that there is a foreign key from ALS_BOOKS to
ALS_PUBLISHERS: every Book has one (and only one) Publisher. We would like
to translate this to our Book and Publisher Entities: Book has a reference
to its Publisher while Publisher has a Books collection of the contents of
Letâ€™s create an attribute Publisher in the Book Entity and letâ€™s generate
getters and setters for this attribute:
Select the getPublisher() method. Now go to the Persistence Properties view
to specify the mapping:
integrates with the RDB component of the Web Tools project for relational
schema information. It is currently necessary to open the Database
Explorer View before creating or selecting a connection in the "Add
Persistence…" wizard. When starting a new workbench session, it is
currently necessary to re-connect to your database if you are working
on-line with the DB. This allows the Dali plug-in to provide DB
related mapping assistance and validation.
The WTP Database Explorer (RDB) â€“ very similar to the Database Connection
Navigator in JDeveloper and the stand-alone Oracle Raptor tool â€“ allows us
to inspect the Tables and the data in them:
Some additional notes from the Getting Started page on the Dali Project site:
- In order to compile EJB3 Entities, it is a requirement that the appropriate jar(s) from an EJB 3.0 runtime are available on the project classpath. The "Add Persistence…" wizard will help you add the required library to your project classpath. You must have previously installed a Java EE 5 or EJB3 persistence runtime implementation–one is not distributed with Dali. This functionality is currently undergoing a change to support libraries so that all the required jars can be easily included in the classpath.
- Dali integrates with the RDB component of the Web Tools project for relational schema information. It is currently necessary to open the Database Explorer View before creating or selecting a connection in the "Add Persistence…" wizard.
- When starting a new workbench session, it is currently necessary to re-connect to your database if you are working on-line with the DB. This allows the Dali plug-in to provide DB related mapping assistance and validation.
- When using WTP 1.0 and higher, the Persistence menu item on a Java project does not display in the J2EE perspective. To work around this, simply switch to the Java Perspective to "Add Persistence…" to the project.
Lots of information and some very instructive Viewlets can be found at: http://www.eclipse.org/dali/gettingstarted.html