-
Notifications
You must be signed in to change notification settings - Fork 105
Description
I have a use case where we need to fetch values from two different tables: entity and entity_case. These tables do not have a foreign key relationship in the database; the relation is purely logical,
The requirement is to:
- Select fields from
entity. - Override the default col values like
labeland many others with a case-specific label fromentity_caseif available. - Apply filters such as soft-delete flags and case code matching.
Example Query:
SELECT ef.name, COALESCE(efc.label, ef.label) .....
FROM entity ef
LEFT JOIN entity_case efc
ON ef.entity_code = efc.entity_code
AND efc.case_code = :caseCode
AND efc.deleted_on IS NULL
AND efc.deleted_user IS NULL
WHERE (ef.case_code = :caseCode OR ef.case_code IS NULL)
AND ef.deleted_on IS NULL
AND ef.deleted_user IS NULL
ORDER BY ef.name
LIMIT :limitEntityView Implamantation:
@EntityView(value = Entity.class)
public interface EntityView {
@IdMapping
String getEntityCode();
@MappingCorrelated(
correlationBasis = "entityCode",
correlator = EntityCaseCorrelator.class,
fetch = FetchStrategy.JOIN)
EntityFieldCaseView getCaseOverride();
@Mapping("COALESCE(caseOverride.label, label)")
// @Mapping("COALESCE(VIEW(caseOverride).label, label)")
// @Mapping("COALESCE(VIEW(caseOverride, label), label)")
String getLabel();Correlator:
public class EntityCaseCorrelator implements CorrelationProvider {
public void applyCorrelation(CorrelationBuilder builder, String correlationExpression) {
final String alias = builder.getCorrelationAlias();
builder.correlate(EntityFieldCase.class)
.on(alias + ".entityCode").eqExpression(correlationExpression)
.on(alias + ".caseCode").eqExpression(":caseCode")
.end();
}
}Problem:
When we are generating/ populating a value using @MappingCorrelator the correlated field (caseOverride.label) cannot be used directly in another column via COALESCE.
I tried multiple variations:
// @Mapping("COALESCE(VIEW(caseOverride).label, label)")
// @Mapping("COALESCE(VIEW(caseOverride, label), label)")…but none worked as expected.
I tried different methods but still not able to resolve this problem.
Fallback Attempt
As a fallback, I tried mapping directly with correlationResult, but this loses the COALESCE behavior:
@EntityView(value = EntityField.class)
public interface EntityFieldView {
@IdMapping
String getEntityFieldCode();
@MappingCorrelated(
correlationBasis = "entityCode",
correlator = EntityCaseCorrelator.class,
fetch = FetchStrategy.JOIN,
correlationResult = "label")
String getLabel();This will give me the value generated using EntityCaseCorrelator, but I loose the COALESCE as in case the EntityCase do not have a value then I need to return the value of column from the Entity table.