Creating Your First Ejb3 Project in Eclipse

This tutorial will teach you on how to setup and run an ejb application. What you need (noted are where I installed my versions): 1.) Jbo...

This tutorial will teach you on how to setup and run an ejb application.

What you need (noted are where I installed my versions):
1.) Jboss 5 (jboss-5.1.0.GA)
2.) eclipse-jee
3.) ojdbc14 (C:\jboss-5.1.0.GA\server\default\lib)

1.) Create a new EJB project.
  a.) In your eclipse select File->New->Other->EJB->EJB Project. I named mine ipielEJB
b.) Click next c.) Click next d.) Check Generate ejb-jar.xml, then click Finish
2.) Add a persistence.xml in your project (inside META-INF folder) (See file below) 3.) Create a new package com.kbs 4.) Create a new class annotate as Entity (See file below) 5.) Create 2 interface BookBeanLocal and BookBeanRemote, annotate with Local and Remote (See files below) 6.) Create a new class BookBean that implements BookBeanLocal and BookBeanRemote. Annotate with Stateless. (See file below) 7.) Create ipielEJB-ds.xml. For format look here: C:\jboss-5.1.0.GA\docs\examples\jca 8.) Our ejb project is almost finish by now. 9.) Configure your jboss server
10.) Deploy ipielEJB to jboss (See screenshot) 11.) You will know if the deployment is successful if you got no error on Console log and can see the following log.

Now to test the EJB bean.
1.) In the same project create a package com.kbs.test
2.) In the newly create package create a class BookClient (See class below)
3.) In ejbModule folder create a new file (See file below)
4.) Run the client. The below screen shot is how the project should look like. Also the Book run log can be seen.
package com.kbs;


import javax.persistence.*;

@Table(name = "book")
@SequenceGenerator(name = "book_sequence", sequenceName = "book_id_seq")
public class Book implements Serializable {
 private static final long serialVersionUID = 7422574264557894633L;
 private Integer id;
 private String title;
 private String author;

 public Book() {

 public Book(Integer id, String title, String author) {
  super(); = id;
  this.title = title; = author;

 public String toString() {
  return "Book: " + getId() + " Title " + getTitle() + " Author "
    + getAuthor();

 public String getAuthor() {
  return author;

 public void setAuthor(String author) { = author;

 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_sequence")
 public Integer getId() {
  return id;

 public void setId(Integer id) { = id;

 public String getTitle() {
  return title;

 public void setTitle(String title) {
  this.title = title;
package com.kbs;

import java.util.Iterator;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class BookBean implements BookBeanLocal, BookBeanRemote {
 EntityManager em;
 public static final String RemoteJNDIName = BookBean.class
   .getSimpleName() + "/remote";
 public static final String LocalJNDIName = BookBean.class
   .getSimpleName() + "/local";

 public void test() {
  Book book = new Book(null, "Hello Java Bean I!", "Edward");
  Book book2 = new Book(null, "Hello Java Bean II", "czetsuya");
  Book book3 = new Book(null, "Hello Java Bean III",
  System.out.println("list some books");
  List someBooks = em.createQuery("from Book b where")
    .setParameter("name", "czetsuya").getResultList();
  for (Iterator iter = someBooks.iterator(); iter.hasNext();) {
   Book element = (Book);
  System.out.println("List all books");
  List allBooks = em.createQuery("from Book").getResultList();
  for (Iterator iter = allBooks.iterator(); iter.hasNext();) {
   Book element = (Book);
  System.out.println("delete a book");
  System.out.println("List all books");
  allBooks = em.createQuery("from Book").getResultList();
  for (Iterator iter = allBooks.iterator(); iter.hasNext();) {
   Book element = (Book);
package com.kbs;

import javax.ejb.Local;

public interface BookBeanLocal {
 void test();
package com.kbs;

import javax.ejb.Remote;

public interface BookBeanRemote {
 void test();
package com.kbs.test;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.kbs.BookBean;
import com.kbs.BookBeanRemote;

public class BookClient {
 public static void main(String[] args) {
  Context context;
  try {
   context = new InitialContext();
   BookBeanRemote beanRemote = (BookBeanRemote) context
  } catch (NamingException e) {
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.0" xmlns=""
Manifest-Version: 1.0
<persistence xmlns=""
 <persistence-unit name="ipielEJB">
   <property name="" value="create-drop" />

Commonly encoutered errors:

1.) Deployment "jboss.jca:name=ejb3ProjectDS,service=DataSourceBinding" is in error due to the following reason(s): ** NOT FOUND Depends on 'jboss.jca:name=ejb3ProjectDS,service=DataSourceBinding' **
Deployment "<UNKNOWN jboss.j2ee:jar=FirstEjb3Tutorial.jar,name=BookTestBean,service=EJB3>" is in error due to the following reason(s): ** UNRESOLVED Demands 'persistence.unit:unitName=#FirstEjb3Tutorial' **
-Simply means your ojdbc14.jar is not where it should be on C:\jboss-5.1.0.GA\server\default\lib

2.) Failed to resolve schema nsURI= location=persistence
-persistence.xml is missing version, xmlns, etc tag

3.) EJBException unknown entity
-Book entity ( is not properly configured

4.) javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
-Missing or unconfigured


javaee 6156056397378204832

Post a Comment Default Comments


Anonymous said...

This is a great post!!! Thanks!!!

Pardhasaradhi said...

Very good post.. Address almost all the queries and the common problems too. Good job.....

Anonymous said...

Thank you very much for your post. I followed all of your steps, however I still encounter the 4th error: javax.naming.NoInitialContextException. I used Jboss6 and Eclipse SDK 3.6.1 + Java EE plugin. Please help me with this error. Thanks in advance

Anonymous said...

i am using jboss 5.1 and trying to deploy ejb3 module in er with war file.. but i am faicng issues " failed to find container meta data for ejb interface"

pls advise

Anonymous said...

the tutorial is very good, Easy to understand. i faced some problem with sequence generator because i am using SQLServer but i was able to resolve it. below are the changes done

in persistence.xml replaced

@TableGenerator(name="TABLE_GEN", table="SEQUENCE_TABLE", pkColumnName="SEQ_NAME",
valueColumnName="SEQ_COUNT", pkColumnValue="EMP_SEQ")

instead of @SequenceGenerator(name = "book_sequence", sequenceName = "book_id_seq")


@GeneratedValue(strategy=GenerationType.TABLE, generator="TABLE_GEN")

instead of @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_sequence")

this resolved my problem but since i changed the dialect i have to create the tables manually

Viji said...

on exectuting BookClient, i get the following error:

javax.naming.NameNotFoundException: BookBean not bound
at org.jnp.server.NamingServer.getBinding(
at org.jnp.server.NamingServer.getBinding(
at org.jnp.server.NamingServer.getObject(
at org.jnp.server.NamingServer.lookup(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at sun.rmi.server.UnicastServerRef.dispatch(
at sun.rmi.transport.Transport$
at sun.rmi.transport.Transport$
at Method)
at sun.rmi.transport.Transport.serviceCall(
at sun.rmi.transport.tcp.TCPTransport.handleMessages(
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(
at sun.rmi.transport.tcp.TCPTransport$
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(
at sun.rmi.transport.StreamRemoteCall.executeCall(
at sun.rmi.server.UnicastRef.invoke(
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(
at org.jnp.interfaces.NamingContext.lookup(
at javax.naming.InitialContext.lookup(
at com.kbs.test.BookClient.main(

Please help.

Unknown said...

I also have the same error like user Vji...
Kindly help out..

Viji said...

I solved the error. In persistence.xml, i changed the data source to DefaultDS as shown below: