I have an existing application with an entity, for example Employee (Table EMP) and it is used at several places. Now after a long time, a replica (EMP_ARC) for the table EMP is created and we are supposed to refer it in certain scenarios.
As the table structure is exactly same I thought of reusing the POJO declaration. But I could not find a way to reuse the same POJO for two different table declarations. (I had seen examples of XML mapping to map one POJO to multiple tables but our application is using annotations and not able to find such option with annotation)
I cannot use Inheritance strategy because polymorphic queries will degrade the performance of the application and the EMP_ARC table has to be referred only when required.
Could anyone please tell me if creating a duplicate POJO and mapping it to the replica table is the only solution or any other approach is available.
**Sample Code:**
Table structure for EMP and EMP_ARC (exactly same structure)
EMP_ID LONG
EMP_NAME VARCHAR2(100)
**Existing Java Bean mapped to EMP**
**This entity bean cannot be modified**
//getter and setters omitted
@Entity
@Table(name="EMP")
public class Employee{
@Id
@Column(name="EMP_ID")
Long empid;
@Column(name="EMP_NAME")
String empName;
}
//Scenario 1 Query EMP table
String hql = "from Employee";
//Scenario 2 Query EMP_ARC table -- Like SELECT * FROM EMP_ARC
//Not sure of how to write the HQL using the same Employee POJO.
//Output of this HQL has to be an Employee POJO.
I want to map the same Employee POJO to the table EMP_ARC also but not able to find a way.
Alternatively, if I can selectively enable/disable polymorphic queries then I will be able to use Inheritance with strategy TABLE_PER_CLASS.
--- Workaround ---
To dynamically enable or disable polymorphic queries, I am using two different session factories.
- I have two POJOs Employee and EmployeeArc (inheriting Employee).
- Inheritance strategy used is TABLE_PER_CLASS.
- One session factory will refer to the Employee entity alone and the other session factory will have both the Employee and EmployeeArc entities.
- Whenever polymorphic query feature is not required then I will use the first session factory, otherwise use the second one.
If anybody is aware of a better solution, please help me in solving this in a better way.