Java EE 5 Persistence

I promised earlier that I will keep you in touch with what new stuff we are working on in Java EE support. Today I would like to write about object relational mapping. This is a very important part of Java EE 5 that we will support in I will post a tutorial about creating simple persistence classes in a javaee5 preview build soon, so take this as a small intro. It is not a complete description of all features, just a quick overview for someone who is not familiar with this area.

Object relational mapping

Object relational mapping (ORM) is about representing data from database as Java beans. The advantage of using ORM over traditional JDBC is that you do not have to think it terms of database tables, columns, foreign keys, etc. All you use is Java beans, properties and collections of other objects.

Java Persistence API

Java Persistence API (JSR 220) is a new standard API for ORM. Unlike the EJB 2.x entity classes the new persistence API does not require the context of an EJB module and can be used in a web module or even in a Java SE application.

Here is how it compares to some other frameworks for accessing data:

  • JDBC is used by most of the other frameworks as the way to access data in relational databases. But it only provides a lower level of abstraction. If you are working with JDBC you are working with database tables and columns. It does not provide ORM.
  • EJB 2.x entity beans provide ORM but the API is complex. NB 5.0 helps you to manage the multiple classes that represent an EJB plus the deployment descriptor together, but the complexity is still there. Also, the EJB 2.1 entities can only be defined in an EJB module and live in an EJB container. Transfering data outside involves data transfer objects which is just an additional level of complexity.
  • Frameworks like Toplink or Hibernate are powerful and developer friendly but are not a JCP standard. These frameworks surely inspired the new standard and are going to be the big players implementing it. Implementation of JSR 220 in GlassFish application server is based on Toplink architecture, the implementation in JBoss 4.0.3 is based on Hibernate.

How does it work

Java Persistence API does not require the persistent classes to implement any special interfaces, they are just "plain old Java objects" (POJO) marked with annotations.

The mapping between the persistent classes and database tables is also controlled by annotations (or an xml file if you prefer that). But most importantly is has some reasonable defaults, for example a persistent class will be mapped to a table of the same name, its properties will be mapped to columns with the same names, etc. Here is a small example:

public class Person {
    private int id;
    private String name;
    private int age;

    @Id(generate = GeneratorType.AUTO)
    public int getId() { 
        return id; 
    public void setId(int id) { = id;
    public String getName() {
        return name;
    public void setName(String name) { = name; 

What if you do need to control the mapping? Let's say the Java class needs to be the same it was but the table is different. You would do something like this:

public class Person {
    @Column (name="EMP_NAME")
    public String getName() {
        return name;

Being POJOs, persistent classes do not need to live within any container. This greatly simplifies writing automated tests or using the persistence classes in combination with other technologies. You can create instances just like this:

   Person p = new Person ();
   p.setId (41);
   p.setName ("Dent Arthur Dent");    

The last nice feature I will mention is that you can write queries to retrieve data from database in terms of Java classes, without reference to the actual relational tables. This is using EJBQL:

      select object(p) from Person as p   

Note that this query will work unchanged even if you map the Person class to table "HR_EMPLOYEES" as shown above.

Persistence API In NetBeans

The basic support for developing persistent classes is in setup and packing of the "Persistence unit" (more about that in the tutorial) and providing templates for persistent classes, code completion for persistence API, etc.

More advanced features we plan to work on include code completion for database metadata (table name, columns, etc.), EJBQL editor, generation of Java classes from an existing database schema, etc.


Hi Pavel, great intro, i am looking forward to the tutorial; there is a small typo above the last code segment (fearure). But the question i have to ask is will Java EE5 be part of the NB final release? (Right now we have to download 2 version the regular and the EE5) that is a pain, will the final release of NB5.0 have EE5 support?

PS:Loking forward to that tutorial

Posted by Daniel MD on nëntor 11, 2005 at 12:40 MD EST #

Hi Daniel. A short answer to your question is no, Java EE 5 support will not be in NetBeans 5.0.

Here is why:
The specifications for Java EE 5 are currently still not completed and the servers are still being developed. NetBeans support for Java EE 5 is still in its diapers, being developed on a branch. Daily builds of Java EE 5 are available for people whole like to look at this and like to live dangerously :-) but they do not have the quality for day to day production use just now. So it will not be part of the NetBeans 5.0 release which is currently close to beta 2 and was feature frozen in September. Java EE 5 will be in the next release after it.
A personal note: I am a developer of netbeans. When I talk about a release that is after feature freeze I have a tendency to call it the 'old release' :-). I am always thinking and looking forward to the next one! That's why I chose to write about future. I understand that most users are still using NetBeans 4.1 and are going to use 5.0 for the next months and it will be a new release for them (there are quite a few cool features in there!). Some are using 4.0 happilly.

Posted by guest on nëntor 11, 2005 at 01:47 MD EST #

OK then :) i am enlightened. This is the Java world at times it can be frustrating sometimes people talk about features that are due in the "Dolphin" release or 2008, i like the fact that i now that it's coming, but sometimes i just wish it was quicker.until then i guess we will have to live in the danger zone ;\^)

Oh Well good things are worth the wait.

Posted by Daniel MD on nëntor 11, 2005 at 03:33 MD EST #

Pavle, great tutorial. Petr

Posted by pblaha on nëntor 13, 2005 at 09:11 PD EST #


Posted by guest on nëntor 17, 2005 at 05:55 MD EST #

hello sir, I would like to know more abt ORM .

Posted by suhail mohd kc on dhjetor 07, 2005 at 06:39 MD EST #

Hey Pavel! Maybe this is not right place to post this comment, but I have one question regarding EJB 3.0 Your blog interested me because I work on similar assignment on my faculty project like the one you are writing about. My assignment is to write code generator which will read EJB 3.0 entities using JAVA reflection and generate Session Beans (methods bodies must also be generated), serves and JSPs. Can you give me an advice how to get that job done or provide me an source code showing me how you did your job. Best regards. Bojan

Posted by Bojan on dhjetor 04, 2006 at 02:09 PD EST #


Posted by z on shkurt 21, 2008 at 04:12 MD EST #

Post a Comment:
  • HTML Syntax: NOT allowed



« qershor 2016