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.
package com.kbs;BookBean.java
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;
}
}
package com.kbs;BookBeanLocal.java
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);
}
}
}
package com.kbs;BookBeanRemote.java
import javax.ejb.Local;
@Local
public interface BookBeanLocal {
void test();
}
package com.kbs;BookClient.java
import javax.ejb.Remote;
@Remote
public interface BookBeanRemote {
void test();
}
package com.kbs.test;ejb-jar.xml
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();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>ipielEJB-ds.xml
<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>
<datasources>MANIFEST.MF
<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-Version: 1.0persistence.xml
Class-Path:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"jndi.properties
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>
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