e premte nëntor 11, 2005 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 (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 (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:
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.
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.