Tuesday, February 14, 2012

First Step in Adf With Ejb 3.0

Hello Reader,

I am going to explain how we can used EJB as business component in adf.



As you all folks  know Adf framework is based on MVC model.By Default we used ADF BC as Model layer (Business component). But we have option to used any Business compoment as a model.So in this basic blog I am try to show all of you how we can used EJB 3.0 as Business component.

Pre Requirment :

Knowledge wise :
1-Basic Knowledge of EJB 3.0
2-Basic knowledge Adf Faces
3-Basic knowledge Adf Binding

Software wise :
1-Jdeveloper 11.1.1.4
2-Oracle XE 10 

I am not going to explain what is EJB and what are type of Ejb.I am just going explain how we can use EJB as Business component in Adf Framwork.

Step 1: Create a new applications and select java EE web application



Step 2: Give the application name according .i have given as EjbWithAdfBasic1 and click next


Step3: In this step we are going to used selected technology as View .By default following technology will selected
1-HTML
2-Java
3-JSF
4-Servlet and JSP

But be care full because we are going to used Adf Faces as View so for this we need to Shuffle   two more technology  from left to right which are Adf page Flow and Adf Faces





Click next
Step 4:Next


Step 5:Next



Application is ready.


It is right to do coding :  :)

Step 6:First we have to create model then View..

Right click om Model
Click on new.As you are know we are going to used EJB as Business component so we need to click on EJB .



Ejb bean is three type

1-Entity Bean
2-Session Bean
3-Message Driven Bean

First we have to create Entity.so for this we need to select Entities From Tables option


Then Ok .


Next




Then again Next


It will be asked for data base connection. Click on + and it will open the Create Connection wizard.



Fill all the value and click on ok .



Click on the Next




Click on query it will show are the available table name in the left panel and select the any table and navigate it to selected



I am using Employee1 table . then select next




if you can see Entity Class options,two type are option are present

1-Fields
2-Methods

By default Fields is selected.I will explain place member-level annotations next upcoming blog for now what  is pre selected we need to go with that.




Then again next





Congratulation ! Our entity is ready to use by Service layer.

It will create the Employee1.java class which is Java representation of the Employee1 Schema.
If you open you can see the following code
package model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

@Entity
@NamedQueries({
@NamedQuery(name = "Employee1.findAll", query = "select o from Employee1 o")
})
public class Employee1 implements Serializable {
@Column(name="ADDRESS_ID")
private Long addressId;
@Id
@Column(name="EMP_ID", nullable = false)
private Long empId;
@Column(name="F_NAME", length = 20)
private String fName;
@Column(name="L_NAME", length = 20)
private String lName;
private Long salary;

public Employee1() {
}

public Employee1(Long addressId, Long empId, String fName, String lName,
Long salary) {
this.addressId = addressId;
this.empId = empId;
this.fName = fName;
this.lName = lName;
this.salary = salary;
}

public Long getAddressId() {
return addressId;
}

public void setAddressId(Long addressId) {
this.addressId = addressId;
}

public Long getEmpId() {
return empId;
}

public void setEmpId(Long empId) {
this.empId = empId;
}

public String getFName() {
return fName;
}

public void setFName(String fName) {
this.fName = fName;
}

public String getLName() {
return lName;
}

public void setLName(String lName) {
this.lName = lName;
}

public Long getSalary() {
return salary;
}

public void setSalary(Long salary) {
this.salary = salary;
}
}

For accessing Entity layer we need to create the service layer .Later client will be consume this layer.

So again click the Model and click on new and select the Ejb and then select the Session Bean then ok



By Default EJB Name is SessionEIB I am going to change it EjbWithAdfSessionEJB


Then next



By default what are method available in the Employee1 it will come here.if you want to see just click on the + which is front of Employee1 .and same time  it will add three more method which are part of Entity Manager.




Then next




I am changing the package model to service .then next




again next




Finally the Ejb session bean has created.It will create three classes


1-EjbWithAdfSessionEJBBean.java

package service;

import java.util.List;

import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.Employee1;

@Stateless(name = "EjbWithAdfSessionEJB", mappedName = "EjbWithAdfBasic1-Model-EjbWithAdfSessionEJB")
public class EjbWithAdfSessionEJBBean implements EjbWithAdfSessionEJB,
EjbWithAdfSessionEJBLocal {
@PersistenceContext(unitName="Model")
private EntityManager em;

public EjbWithAdfSessionEJBBean() {
}

public Object queryByRange(String jpqlStmt, int firstResult,
int maxResults) {
Query query = em.createQuery(jpqlStmt);
if (firstResult > 0) {
query = query.setFirstResult(firstResult);
}
if (maxResults > 0) {
query = query.setMaxResults(maxResults);
}
return query.getResultList();
}

public Employee1 persistEmployee1(Employee1 employee1) {
em.persist(employee1);
return employee1;
}

public Employee1 mergeEmployee1(Employee1 employee1) {
return em.merge(employee1);
}

public void removeEmployee1(Employee1 employee1) {
employee1 = em.find(Employee1.class, employee1.getEmpId());
em.remove(employee1);
}

/** select o from Employee1 o */
public List getEmployee1FindAll() {
return em.createNamedQuery("Employee1.findAll").getResultList();
}
}



2-EjbWithAdfSessionEJBLocal.java



import java.util.List;

import javax.ejb.Local;

import model.Employee1;

@Local
public interface EjbWithAdfSessionEJBLocal {
Object queryByRange(String jpqlStmt, int firstResult, int maxResults);

Employee1 persistEmployee1(Employee1 employee1);

Employee1 mergeEmployee1(Employee1 employee1);

void removeEmployee1(Employee1 employee1);

List getEmployee1FindAll();
}





3-EjbWithAdfSessionEJB.java

package service;

import java.util.List;

import javax.ejb.Remote;

import model.Employee1;

@Remote
public interface EjbWithAdfSessionEJB {
Object queryByRange(String jpqlStmt, int firstResult, int maxResults);

Employee1 persistEmployee1(Employee1 employee1);

Employee1 mergeEmployee1(Employee1 employee1);

void removeEmployee1(Employee1 employee1);

List getEmployee1FindAll();
}





Entity and Service layer have created. This is right time to expose this layer to client .For exposing this layer to client we need to create data control .So I am going to create the Data Control using Session bean .Right click on the EjbWithAdfSessionEJBBean.java



Click on the Create Data Control it will create Data control for this session Bean .




It will asked for interface .By default local will selected then click on Ok .

It will create the data control .just click on data control and you are able to see the New data control.




Form here it will same as what we used to do with Adf Bc. Just Create a jsf page and drag and drop any method which are present in the data control layer.

You can also crate client class to check whether Service layer is working fine or not .For this just create a
Client class.
 
Creating the Client Class just right on the EjbWithAdfSessionEJBBean.java and than select New sample java client.



it will create a java class name  as EjbWithAdfSessionEJBClient.java. just run it and see is the session layer 
working fine or not.Code sample is following




package model;

import java.util.Hashtable;
import java.util.List;

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

import javax.naming.NamingException;

import service.EjbWithAdfSessionEJB;

public class EjbWithAdfSessionEJBClient {
public static void main(String [] args) {
try {
final Context context = getInitialContext();
EjbWithAdfSessionEJB ejbWithAdfSessionEJB = (EjbWithAdfSessionEJB)context.lookup("EjbWithAdfBasic1-Model-EjbWithAdfSessionEJB#service.EjbWithAdfSessionEJB");
for (Employee1 employee1 : (List)ejbWithAdfSessionEJB.getEmployee1FindAll()) {
printEmployee1(employee1);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

private static void printEmployee1(Employee1 employee1) {
System.out.println( "addressId = " + employee1.getAddressId() );
System.out.println( "empId = " + employee1.getEmpId() );
System.out.println( "fName = " + employee1.getFName() );
System.out.println( "lName = " + employee1.getLName() );
System.out.println( "salary = " + employee1.getSalary() );
}

private static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
// WebLogic Server 10.x connection details
env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
return new InitialContext( env );
}
}






I hope it  will be help full for you.

Caveat :It required Java ,EJB and ADf knowledge. 

Thanks