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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBWt7rUi8fSzqjpHHyEV8cj6lPCjV1KX5UkLMPWE5EntlG7z6u0yf2tq5EnqJSKDaUBQGsiBPVT9fl6l6p_QB3MySIoKT1wZ5L2yvol0zxz5gqG2gaperIRcOuWXkV6MqQ3z6VelC6ViSi/s320/eclipselink_14.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYaXC0nFzPmsyUogHm_cH08jAnfGC3RIVZx9CY2Q7gHk_SJzDOfWzsyyUM6boCzfQ7LoSB2ApAdqaZaWcTNHxspwR96kRyVe9A8c0UWRQ_i7NOsq9P5XhUYtQE5U4fj3wH3i9PW5SufQ_6/s320/eclipselink_16.png)
Add the relations manually between the department and employee entities.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqjI6MKSpO6fdpS7tmaE9PUpiO2hEklrTzHOT1kHiFZMvV2yQk8lC9yqdx0bpkFH3zDJasDejRkF1y2o6i5cppe9_9rKiMIjCYmHGlAa504SaHeK3Ok06B_DhOacUXZgHsK3SMKeWemlXv/s320/eclipselink_19.png)
Add the Oracle sequences definitions to the primary keys
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwfuOdZCoe3y7Dgpyjff5cvs-dCK4H1HOnY4KjDTWE_v6bVScixYy8j20k1uWsYWM7ydr1heDWcOfguCW4vC82DGaMHr-kHF3sUl2jo8CkRZ4ED4mlhFtvwJ_TEu1cOiRnYhVBudQeto-R/s320/eclipselink_24.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigid0vNj3olm1YxwCYwU_dN3zNZwPyLwCs_9k1GLX2GCgjFGhAitbo5IRz9na-q2PR-UoLaEcxZcUlU9dy5XqmIh0KlChUjTjqWhMJJ2h-rusDqVPqRiE4WbpWxKZ-AjsN8HiFEXxkV9A5/s320/eclipselink_26.png)
Create an session bean
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXOir-807qOn3y14kX5vhyphenhyphenPyWGRaEOb69ayh_O09PRM87I2s14nFfPMy6YsIpgvmS5X-GZ0u3G1KjxMrD3hxAFWValmtivUYQP5MJHvCks6W4lB4KwlypkA8Ki0RV5-7LKkQAujOwo0VzY/s320/eclipselink_95.png)
And make an EJB client to test the session.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcyycLv0rDT6onBIoj_uBnlbK6Z-5xgi46azgt-DXXwH_T8xKyhPLirowsvpX9BkzYoyHCwfeTcesRA-QCimamL9NqpFqUErAS5aPO22FW9OgKtEDO4tg5NpCNsM3U5okvRvxgrbkCTiAq/s320/eclipselink_99.png)
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