Creating your first EJB3 Project in eclipse on JBoss Server
Tech-Today

Creating your first EJB3 Project in eclipse on JBoss Server


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)

Steps:
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 Book.java 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 jndi.properties (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.

Book.java
package com.kbs;

import java.io.Serializable;

import javax.persistence.*;

@Entity
@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() {
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;
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_sequence")
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;
}
}
BookBean.java
package com.kbs;

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

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

@Stateless
public class BookBean implements BookBeanLocal, BookBeanRemote {
@PersistenceContext
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");
em.persist(book);
Book book2 = new Book(null, "Hello Java Bean II", "czetsuya");
em.persist(book2);
Book book3 = new Book(null, "Hello Java Bean III",
"ipiel");
em.persist(book3);
System.out.println("list some books");
List someBooks = em.createQuery("from Book b where b.author=:name")
.setParameter("name", "czetsuya").getResultList();
for (Iterator iter = someBooks.iterator(); iter.hasNext();) {
Book element = (Book) iter.next();
System.out.println(element);
}
System.out.println("List all books");
List allBooks = em.createQuery("from Book").getResultList();
for (Iterator iter = allBooks.iterator(); iter.hasNext();) {
Book element = (Book) iter.next();
System.out.println(element);
}
System.out.println("delete a book");
em.remove(book2);
System.out.println("List all books");
allBooks = em.createQuery("from Book").getResultList();
for (Iterator iter = allBooks.iterator(); iter.hasNext();) {
Book element = (Book) iter.next();
System.out.println(element);
}
}
}
BookBeanLocal.java
package com.kbs;

import javax.ejb.Local;

@Local
public interface BookBeanLocal {
void test();
}
BookBeanRemote.java
package com.kbs;

import javax.ejb.Remote;

@Remote
public interface BookBeanRemote {
void test();
}
BookClient.java
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
.lookup(BookBean.RemoteJNDIName);
beanRemote.test();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<display-name>ipielEJB</display-name>
</ejb-jar>
ipielEJB-ds.xml
<datasources>
<local-tx-datasource>
<jndi-name>ipielEJBDS</jndi-name>
<connection-url>jdbc:oracle:thin:@localhost:1521:xe</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>demo</user-name>
<password>demo</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter
</exception-sorter-class-name>
<metadata>
<type-mapping>Oracle9i</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
MANIFEST.MF
Manifest-Version: 1.0
Class-Path:
persistence.xml
<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_1_0.xsd"
version="1.0">
<persistence-unit name="ipielEJB">
<jta-data-source>java:/ipielEJBDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

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 (Book.java) 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 jndi.properties




- Hibernate Search Faceting
With the document available from Hibernate Search (https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#query-faceting), we should be able to implement a faceting example that returns the faceting field and its count. In the example...

- How To Implement Jms Request/response Feature In Glassfish
This tutorial will provide a sample code on how you can send request and receive reply using jms. I've used Glassfish 3.1.1, with its built in JMS Broker for testing set to LOCAL. We need 2 maven projects (1 is for request and 1 for receiving the...

- How To Call A Stateless Ejb From A Spring Rest Servlet Component
In my recent project I was ask to develop a rest servlet (using jersey) in a spring web project that would call an stateless EJB, where the business logic is.  This project is deployed on glassfish. After several hours I was able to figure out how...

- Create A Primary Key Column With Sequence Generated Value In Hibernate
For example you have a class User: package org.kalidad.seamexercises.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.SequenceGenerator; ...

- Creating Your First Enterprise Application Project On Eclipse-jee-helios
First you should create an ejb project as specified here: http://czetsuya-tech.blogspot.com/2011/03/creating-your-first-ejb3-project-in.html Take note that my interface were inside the ejb project. You can pull that out and place in its own project ipielEJB2Client....



Tech-Today








.