I suspect there’s no perfect solution to this problem so least worst solution are more than welcome.
I’m implementing a dashboard using PrimeFaces and I would like to persist the model backing it (using JPA2). I’ve written my own implementation of DashboardModel and DashboardColumn with the necessary annotations and other fields I need. The model is shown below:
@Entity
public class DashboardSettings implements DashboardModel, Serializable{
@Id
private long id;
@OrderColumn( name="COLUMN_ORDER" )
private List<DashboardColumn> columns;
...a few other fields...
public DashboardSettings() {}
@Override
public void addColumn(DashboardColumn column) {
this.columns.add(column);
}
@Override
public List<DashboardColumn> getColumns() {
return columns;
}
...snip...
}
The problem is the columns field. I would like this field to be persisted into it’s own table but because DashboardColumn is an interface (and from a third party so can’t be changed) the field currently gets stored in a blob. If I change the type of the columns field to my own implementation (DashboardColumnSettings) which is marked with @Entity the addColumn method would cease to work correctly – it would have to do a type check and cast.
The type check and cast is not the end of the world as this code will only be consumed by our development team but it is a trip hazard. Is there any way to have the columns field persisted while at the same time leaving it as a DashboardColumn?
You can try to use
targetEntityattribute, though I’m note sure it would be better than explicit cast: