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.


Add the relations manually between the department and employee entities.

Add the Oracle sequences definitions to the primary keys


Create an session bean

And make an EJB client to test the session.

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;
import nl.whitehorses.eclipselink.model.services.HrSession;
public class HrSessionClient {
public static void main(String [] args) {
try {
final Context context = getInitialContext();
HrSession hrSession = (HrSession)context.lookup("Eclipselink-HrSession#nl.whitehorses.eclipselink.model.services.HrSession");
// insert new department
Departments department = new Departments();
department.setDepartmentName("Sales");
department.setLocationId(1700L);
department.setManagerId(200L);
department = (Departments)hrSession.mergeEntity(department);
System.out.println("new department id: "+department.getDepartmentId());
// add a new employee
Employees employee1 = new Employees();
employee1.setEmail("Test1@test.nl");
employee1.setLastName("Test1");
employee1.setJobId("ST_MAN");
employee1.setHireDate(new Timestamp(new Date().getTime()));
department.addEmployees(employee1);
department = (Departments)hrSession.mergeEntity(department);
// add a other new employee
Employees employee2 = new Employees();
employee2.setEmail("Test2@test.nl");
employee2.setLastName("Test2");
employee2.setJobId("ST_MAN");
employee2.setHireDate(new Timestamp(new Date().getTime()));
employee2.setDepartment(department);
department.getEmployees().add(employee2);
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) {
ex.printStackTrace();
}
}
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 );
}
}
No comments:
Post a Comment