EJB3-Create an Entity Bean

Understand how an Entity bean works in EJB 3. For this let’s first create a project.

Create a new project with MyEclipse

We will use the project name as FirstEjb3Tutorial.

We’ll be using Entity beans and we will use kind of datasource. This can be configured in persistence.xml.

Let’s create an xml file called persistence.xml in the folder META-INF. JBoss supports the tag hibernate.hbm2ddl.auto to define if your tables are created or updated during redeployment. We chose to create-drop to have them dropped after each undeployment, so that they can be nicely recreated. The option update does not work sometimes.

<span style="font-style: normal;"> java:/ejb3ProjectDS </span>

Add the necessary libraries to the project

As we  need libraries during development of ejb3 and some for using a remote client to test our application later on, we can put all the libraries after downloading in one location.

Download JBoss EJB3 at http://www.jboss.org/products/list/downloads

All  the libraries that we needed can be taken from this package.

After this have a look into your jboss directory. We need the jbossallclient.jar and the jboss.jar.

When we are working under Windows, the directory will look like this.

E:\jboss-4.0.4RC1\client

There is a chance that we select many libraries. See if you can delete one.

Entity Bean – Creation

Let’s create a new class Book in the package com.home.library

Add the attributes

private Integer id;
private String title;
private String author;

Select Generate Getter/Setter from the Source Menu. There is a function in by which you generate the getter/setter – you can reach the function with Alt+Shift + S or with the context menu (right mouse click) of the source code. Add the following constructors and implement the toString method. (Alt+Shift+S +Override/Implement methods). This can be useful while debugging.

span style="font-style: normal;">public </span><span style="font-style: normal;">Book() { super(); } public Book(Integer id, String title, String author) { super(); this.id = id; this.title =title;this.author = author; } 
</span>
@Override
public String toString() {
return "Book: " + getId() + " Title " + getTitle() + " Author "
+ getAuthor();
}

Implement the interface java.io.Serializable. It is a marker interface. This means you do not have to implement any methods (normally).Finally, this is our full source code now:

package com.home.library;
import java.io.Serializable;
public class Book implements Serializable {
private static final long serialVersionUID = 2322574264557294633L;
private Integer id;
private String title;
private String author;
public Book() {
super();
}
public Book(Integer id, String title, String author) {
super();
this.id = id;
this.title = title;
this.author = author;
}
@Override
public String toString() {
return "Book: " + getId() + " Title " + getTitle() + " Author "
+ getAuthor();
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

It’s better to use an empty constructor and create other as needed. Implement the interface java.io.Serializable as entity beans are frequently serialized by caches, by the entity manager etc.It is better to  overwrite the ‘toString’ method because a meaningful output is useful for debugging

How to add the Annotations

@Entity
@Table(name="book")
@SequenceGenerator(name = "book_sequence", sequenceName = "book_id_seq")
public class Book implements Serializable {

Entity defines that this is an entity bean. The second defines the table name. The last one defines a sequence generator. Primary keys can be generated in different ways: You can assign them. For example a language table and the primary key is the ISO-Country code id: EN,DE,FR,

Use a sequence for PostgreSql, SapDb, Oracle and other . A sequence is a database feature. It returns the next Integer or Long value each time it is called.

In MsSql and other you can use identity.

Sequence primary key

As we are using PostgreSql, we defined the sequence first in order to use it later for the primary key. In front of the getId I configure the ID and the generation approach.

<span style="font-style: normal;">
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_sequence")
public Integer getId() { 
return id; 
}</span>

Important – generator = “book_sequence” referes to the named defined in front of your class

Identity primary key

For MSSql Server you will probably only need

@<span style="font-style: normal;">Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
public Integer getId() {
return id; }</span>

Table based primary key

 

Here is one solution that always works: It safes the primary keys in a separated table. One row for

each primary key. Define it in front of your class:

@TableGenerator( name="book_id", table="primary_keys", pkColumnName="key",
pkColumnValue="book", valueColumnName="value")

and use it:

@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "book_id")
public Integer getId() {

Important

generator = "<strong>book_id </strong>referes to the name defined in front of your class @TableGenerator(
name="<strong>book_id</strong>"

JNDI data source

Download the database driver and put it into JBOSS_HOME\server\default\lib. Restart the

server. Create a file named myFavouriteName-ds.xml. There is a naming convention. Please ensure to keep  the bold text. You can find a lot of examples for different databases in the installation path of JBoss.

JBOSS_HOME/docs/examples/jca

A datasource for PostgreSql looks like

<?xml ver<span style="font-style: normal;">sion="1.0" encoding="UTF-8"?>
<datasources> <local-tx-datasource> <jndi-name>ejb3ExampleDS</jndi-name> <connection-url>jdbc:postgresql://localhost:5432/examples</connection-url>
<driver-class>org.postgresql.Driver</driver-class> <user-name>postgres</user-name>
<password>p</password> <!-- the minimum size of the connection pool -->
<min-pool-size>1</min-pool-size> <!-- The maximum connections in a pool/sub-pool -->
<max-pool-size>4</max-pool-size> </local-tx-datasource></span>