I currently have two entities that inherit from the same class, say B and C inheriting from class A. I am currently using joined inheritance which means for of the classes A, B and C, I have the corresponding tables in the Database. B and C have different columns.
Here are the two questions I have:
Displaying the information
I would like to have one table to show both B and C. However the rendering of B and C is specific.
In the table above B.C1 means table B, attribute C1. C.C3 means table C, attribute C3.
How can I extend the Table to show rows containing B differently than rows of C?
Edition
My plan is to have two buttons “Add A” and “Add B”, when the user chooses to create a new A, I create the corresponding form at runtime with the required fields of A. The same approach for B.
Since both entities have the same parent, JPQL query that loads parent entity - loads all children as well. But in this case, you have no access to the specific attributes of child entities. I can suggest implementing a LoadDelegate that loads children entities separately and add them to a single collection:
@Install(to = "customersDl", target = Target.DATA_LOADER)
private List<Customer> customersDlLoadDelegate(LoadContext<Customer> loadContext) {
List<Individual> individuals = dataManager.loadList(
LoadContext.create(Individual.class)
.setQuery(LoadContext.createQuery("select e from demo_Individual e"))
);
List<Company> companies = dataManager.loadList(
LoadContext.create(Company.class)
.setQuery(LoadContext.createQuery("select e from demo_Company e"))
);
return ListUtils.union(individuals, companies);
}
In order to show specific attributes, you need to create generated columns:
@Subscribe
public void onInit(InitEvent event) {
customersDataGrid.addGeneratedColumn("age", new DataGrid.ColumnGenerator<Customer, Integer>() {
@Override
public Integer getValue(DataGrid.ColumnGeneratorEvent<Customer> event) {
return (event.getItem() instanceof Individual) ? ((Individual) event.getItem()).getAge() : null;
}
@Override
public Class<Integer> getType() {
return Integer.class;
}
});
customersDataGrid.addGeneratedColumn("address", new DataGrid.ColumnGenerator<Customer, String>() {
@Override
public String getValue(DataGrid.ColumnGeneratorEvent<Customer> event) {
return (event.getItem() instanceof Company) ? ((Company) event.getItem()).getAddress() : null;
}
@Override
public Class<String> getType() {
return String.class;
}
});
}
In order to create a specific entity, you need to subscribe to ActionPerformedEvent of a corresponding CreateAction and provide a custom implementation, e.g.: