Entity inheritance
hypnotik Oct 19, 2005 7:39 AMI have the following entities:
package hr.ursic.timesheet.entity; import java.util.*; import javax.persistence.*; /** * The Charge entity is an abstract superclass for WorkCharge and ExpenseCharge entities. * It is abstract as charges must be either work charges or expense charges and can't be * instantiated as generic charges. * A JOINED inheritance strategy is used so that a table for Charge entity stores common * fields for both work and expense charges while their specific fields are stored in tables * specific for WorkCharge and ExpenseCharge entities. * Each charge belongs to a single Employee entity and this relationship is specified on * the Charge entity. This relationship is Many-To-One. * */ @Entity @Table(name="charge") @Inheritance(strategy=InheritanceType.JOINED) public abstract class Charge { // properties protected Long id; protected Date date; // relations protected Employee employee; protected Project project; // accessor methods for charge id @Id (generate=GeneratorType.AUTO) @Column (name="id") public Long getId(){ return id; } public void setId(Long id){ this.id = id; } // accessor methods for charge date @Column (name="charge_date") public Date getDate(){ return date; } public void setDate(Date date){ this.date = date; } // accessor methods for employee @ManyToOne @JoinColumn (name="employee") public Employee getEmployee(){ return employee; } public void setEmployee(Employee employee){ this.employee = employee; } }
package hr.ursic.timesheet.entity; import hr.ursic.timesheetEJB.WorkChargeDetail; import javax.persistence.*; /** * WorkCharge is a concrete subclass of Charge entity and represents work charges. Since * JOINED inheritance strategy is used, a table for WorkCharge entity will hold only fields * specific for work charges. The common fields are stored in Charge entity table. * * */ @Entity @Table(name="work_charge") public class WorkCharge extends Charge { // properties private Double time; // relations protected Activity activity; // accessor methods for work charge time @Column (name="charge_time") public Double getTime(){ return time; } public void setTime(Double time){ this.time = time; } // retrieve work charge details @Transient public WorkChargeDetail getDetails(){ WorkChargeDetail workChargeDetail = new WorkChargeDetail(); workChargeDetail.setEmployeeId(getEmployee().getId()); workChargeDetail.setDate(this.getDate()); return workChargeDetail; } public void setDetails(WorkChargeDetail workChargeDetail){ setDate(workChargeDetail.getDate()); setTime(workChargeDetail.getTime()); } }
package hr.ursic.timesheet.entity; import hr.ursic.timesheetEJB.ExpenseChargeDetail; import javax.persistence.*; /** * ExpenseCharge is a concrete subclass of Charge entity and represents expense charges. Since * JOINED inheritance strategy is used, a table for ExpenseCharge entity will hold only fields * specific for expense charges. The common fields are stored in Charge entity table. * * */ @Entity @Table(name = "expense_charge") public class ExpenseCharge extends Charge { // properties private Double amount; // relations private ExpenseType expenseType; // accessor methods for expense charge amount @Column (name="amount") public Double getAmount() { return amount; } public void setAmount(Double amount) { this.amount = amount; } // retrieve work charge details @Transient public ExpenseChargeDetail getDetails(){ ExpenseChargeDetail expenseChargeDetail = new ExpenseChargeDetail(); expenseChargeDetail.setEmployeeId(getEmployee().getId()); expenseChargeDetail.setDate(this.getDate()); return expenseChargeDetail; } }
The question is: if I have id for the Charge how can I determine if the Charge is in fact WorkCharge or ExpenseCharge?