I have designed a class which represents a historical snapshot of an employee, and I am trying to find the best way to map it. The intent of this class is to be able to retrieve various changing properties of an employee, given a retrieval date. The changing properties are spread across several tables, or are generated by procedures:
Code:
public class TransitoryEmployee implements Serializable {
//the employee number and retrieval timestamp
//are used to generate this object from the DB
private int employeeNbr;
private DateTime retrievalTmstp;
private LocalDate hireDate; //non-changing
private LocalDate birthDate; //non-changing
private int ageYearsAmt; //generated
private LocalDate seniorityDate; //tracked in a history table
private CrewPosition crewPosition; //tracked in a history table
private String payStatusCode; //tracked in a history table
private int payRateCentsAmt; //hourly pay rate in cents, generated by procedure
...
}
There is no database table that this class can be mapped to; it is read-only by nature. I have written a single query which can populate this class, and takes the arguments employeeNbr and retrievalTmstp.
How can I incorporate this query into a hibernate mapping file? I am considering two options right now, but would appreciate other ideas.
Option 1. Write a mapping file with a composite ID of employeeNbr and retrievalTmstp, using a native SQL query as a loader. This feels unnatural, and I've encountered lots of problems attempting to implement it.
Option 2. Use filters, similar to the example from section 17 of the Hibernate documentation. However, I am not sure how to make this work. A filter would need to be applied for the retrieval timestamp, but this class can not be created in absence of a filter. Can Hibernate force a filter to be used? Also, when using the filter, how can the object field retrievalTmstp be populated using the value being passed to the filter?
Are either or these options practical? Is the whole idea of a historical snapshot class a bad idea? I'd love to get some input from somebody who has done something similar to this before.