Monday, April 6, 2009

Create a 11G EJB Project with EclipseLink

In JDeveloper 11G you can off course create an ADF BC model project for your ADF Webapp but why don't you use an EJB model project with EclipseLink, it is not so difficult. For this blog entry I made a document with the screenshots of the step you need to do to make an EJB model project. After this you can read this blog how to use this in ADF.

Here are the steps I did for this model project based on the HR demo schema.
The first step is to create the EJB project and generate the entity classes.
Change the database and datasource settings in the persistence xml.

Add the relations manually between the department and employee entities.

Add the Oracle sequences definitions to the primary keys
Add a new named query to the entity

Create an session bean

And make an EJB client to test the session.
Here is the example workspace and the document with all the steps and here is the code of my test client where I create a department with two employees.

package nl.whitehorses.eclipselink.model.client;

import java.sql.Timestamp;

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

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

import javax.naming.NamingException;

import nl.whitehorses.eclipselink.model.entities.Departments;
import nl.whitehorses.eclipselink.model.entities.Employees;

public class HrSessionClient {
public static void main(String [] args) {
try {
final Context context = getInitialContext();
HrSession hrSession = (HrSession)context.lookup("");

// insert new department
Departments department = new Departments();
department = (Departments)hrSession.mergeEntity(department);
System.out.println("new department id: "+department.getDepartmentId());

// add a new employee
Employees employee1 = new Employees();
employee1.setHireDate(new Timestamp(new Date().getTime()));

department = (Departments)hrSession.mergeEntity(department);

// add a other new employee
Employees employee2 = new Employees();
employee2.setHireDate(new Timestamp(new Date().getTime()));

department = (Departments)hrSession.mergeEntity(department);

// retrieving the new department with its employees
for (Departments departments : (List<Departments>)hrSession.queryDepartmentsFindOne( department.getDepartmentId())) {
System.out.println( "departmentId = " + departments.getDepartmentId() );
System.out.println( "departmentName = " + departments.getDepartmentName() );
System.out.println( "locationId = " + departments.getLocationId() );
System.out.println( "managerId = " + departments.getManagerId() );
for (Employees employees : departments.getEmployees()) {
System.out.println( "employeeId = " + employees.getEmployeeId() );
System.out.println( "commissionPct = " + employees.getCommissionPct() );
System.out.println( "email = " + employees.getEmail() );
System.out.println( "firstName = " + employees.getFirstName() );
System.out.println( "hireDate = " + employees.getHireDate() );


} catch (Exception ex) {

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://");
return new InitialContext( env );

No comments:

Post a Comment