I had worked at legacy code to migrate from Spring POJO into EJB3. I try to do ORM with Composite Design Pattern. Here is my solution. Suppose I want to have employee and their direct manager at report. So I create a Composite Design pattern that has abstract class EmplyeeComponent, Employee and Manager.

(Reference: A look at the Composite design pattern)

For EJB3 entity bean: use Many-To-One self referencing relationship.

abstract class EmployeeComponent implements Serializable {
private String name;

public String getName();

}

@Entity

@Table(name=”EMPLOYEE”)

public class Employee extends EmployComponet{

@Id

@Column(name=”EMPLOYEE_ID”)

protected Long employeeId;

@ManyToOne

@JoinColumn(name=”MANAGER_ID”,

referencedColumnName=”EMPLOYEE_ID”)

Manager manager;

}

@Entity

@Table(name=”EMPLOYEE”)

public class Manager extends EmployComponet{

@Id

@Column(name=”EMPLOYEE_ID”)

protected Long managerId;

}

Table design:

Database hierarchy query:

select a.Employee_ID employeeId, b.Employee_ID managerId
from employee a, employee b
where a.MANAGER_ID = b.Employee_ID

Advertisements