views:

32

answers:

0

I have two separate databases that contain a report and reportItems table. The table structure is the same for both databases.

I have a session bean that needs to write to each database / table. In order to keep the code simple I want the session bean to work with a base entity bean that then goes to the appropriate table.

To accomplish that, I've created a base entity class call Report and another called ReportItem. The Report class contains a OneToMany relation for the ReportItem class.

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Report implements Serializable {
  ...    

  @OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL},targetEntity=ReportItem.class)
  @JoinColumn(name="Report_ID", nullable=false)
  private Collection<ReportItem> reportItems = new ArrayList<ReportItem>();

... (set all the other properties)

}

From the Report and ReportItem classes I've descended two additional classes.

Report -> Database1Report & Database2Report

ReportItem -> Database1ReportItem & Database2ReportItem

The base class does not contain a @Table because I really don't want it to write to the database so I declare it in the descendant classes accordingly.

@Entity
@Table(schema= "Database1", name = "reports")
public class Database1Report extends Report implements Serializable {
    public Database1Report() {}
}

The problem is that now I need my Database1Report class to use Database1ReportItem for it's collection. Without being able to override the targetEntity in the descendant class I don't know how to change the collection that was defined in the base entity class.

I'd like to do something like the AssociationOverrride but it doesn't work for targetEntity

@AssociationOverride( name="report", targetEntity=Database1Report.class)

Any suggestions?