A major enhancement in EJB technology is the addition of the new Java Persistence API (JPA), which simplifies the Entity persistence model and adds capabilities that were not in EJB 2.1 technology. The Java Persistence API draws on ideas from leading persistence frameworks and APIs such as Hibernate, Oracle TopLink, and Java Data Objects (JDO), and well as on the earlier EJB container-managed persistence.

 

With Java EE 5, most of the XML configurations have been relegated to annotations.

Like the SLSB, the Entity is going to be a POJO, which will help provide us the most flexibility when creating the domain there. The Entity will look like a regular JavaBean representing a table in the database. Each of the properties will be assumed to be a persistable column on the database. This will then allow us to use EntityManager (which we will discuss in a bit) to perform operations on it that will be translated to our table.

EntityManager can be called from any class, but preferably we would want to call it from a Session Bean implementing the well famous Session Facade pattern.

So take for example the Class Note which contains some information about a trouble ticketing system

package com.sample;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "NOTE")

public class Note implements Serializable {
long noteId;

String text;

String actor;

String attachment;

@Id
@GeneratedValue
public long getNoteId() {
 return noteId;
}

public void setNoteId(long noteId) {
 this.noteId = noteId;
}

public String getActor() {
 return actor;
}

public void setActor(String actor) {
 this.actor = actor;
}

public String getAttachment() {
 return attachment;
}

public void setAttachment(String attachment) {
 this.attachment = attachment;
}

public String getText() {
 return text;
}

public void setText(String text) {
 this.text = text;
}
}

Here you can see a few annotations: at fist the Entity annotation which declares the Bean as Entity Bean. Then we have the Table annotation which is not mandatory but usually useful because naming conventions for DB Tables and Java classes usually don't collide. Let's go on: The @Id annotations defines the field as the primary key of the table: s.

What's the right place for the @Id tag ? You can either add the annotation above the class field or on the getter method of the field. If you place it on the setter method it will be ignored.

Along with the @Id there's the @GeneratedValue annotation. This is used with the @Id annotation when you have a value that is generated. The following are the types of strategies allowed (the default is AUTO):

AUTO: Indicates that the database should automatically pick the appropriate type
TABLE: Indicates that the database should have an underlying table referencing this unique ID
IDENTITY: Indicates that the database should use this field as an identity column
SEQUENCE: Indicates that the database should use a sequence column to get the column

If you need an example of an Entity structured with a Sequence here's one short tutorial . Aren't we forgetting something ? what about column name mappings ? well as default the Container will try to map the EJB field with Database table name if they are the same. However, if you need to have a different name, you can use the @Table annotation.
So if your text field were named TICKETTEXT in your table , your class would look like:

@Column(name="TICKETTEXT")
  public String getText() {
  return text;
}

How do you connect to your DataSource?

Just write a simple file named persistence.xml file which defines the provider for the data source. In this case we suppose we have a Datasource registered with the Jndi Name"ORACLEDS". Since we are using JBoss as the container, we will be using the Hibernate persistence provider. Then we provide the JNDI name so that the application server can provide a name to the associated data source instance classes. 

<persistence>

 <persistence-unit name="userDatabase">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <jta-data-source>ORACLEDS</jta-data-source>
    <properties>
      <property name="hibernate.dialect" 
       value="org.hibernate.dialect.OracleDialect"/>
    </properties>
 </persistence-unit>
</persistence>

Important Update: When using JBoss AS 5.x - 6.x persistence.xml needs to conform to a stricter syntax, which includes schema location and persistence version. Here's how you would connect using JPA 2 on JBoss AS 6

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">

 <persistence-unit name="userDatabase">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <jta-data-source>ORACLEDS</jta-data-source>
    <properties>
      <property name="hibernate.dialect" 
       value="org.hibernate.dialect.OracleDialect"/>
    </properties>
 </persistence-unit>
</persistence>
0
0
0
s2smodern