Pavel Buzek's Weblog

All | JavaEE5 | netbeans
« NetBeans Module... | Main | Java EE 5 Persistenc... »
20051111 e premte nëntor 11, 2005

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 netbeans.next. 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:

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:

CREATE TABLE APP.PERSON
(
ID INT NOT NULL,
NAME VARCHAR(40),
AGE INT NOT NULL
)
...
@Entity
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) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.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:

CREATE TABLE APP.HR_EMPLOYEE
(
ID INT NOT NULL,
EMP_NAME VARCHAR(40),
EMP_AGE INT NOT NULL
)
...
@Entity()
@Table(name="HR_EMPLOYEES")
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.


( Nën 11 2005, 08:02:27 MD EST ) Permalink Comments [8]

Trackback URL: http://blogs.sun.com/pavel/entry/java_ee_5_persistence
Comments:

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 08: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 192.18.42.11 on nëntor 11, 2005 at 09: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 11:33 MD EST #

Pavle, great tutorial. Petr

Posted by pblaha on nëntor 13, 2005 at 05:11 MD EST #

nb

Posted by 61.95.201.104 on nëntor 18, 2005 at 01:55 PD EST #

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

Posted by suhail mohd kc on dhjetor 08, 2005 at 02:39 PD 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 10:09 PD EST #

x

Posted by z on shkurt 22, 2008 at 12:12 PD EST #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed
Download NetBeans IDE

Calendar

RSS Feeds

Recent Entries

Blogs

Search

Navigation

Referers