When working with a legacy database, there are cases when I can't set up the relations properly using belongs_to
annotation. In this case, I tried to define an attribute pointing to another Model
class with its accessors methods as follows:
@Table("INTERVENTION")
@IdName("ITV_ID")
public class Intervention extends Model {
private InterventionModel interventionModel;
public InterventionModel getInterventionModel() {
return interventionModel;
}
public void setInterventionModel(InterventionModel interventionModel) {
this.interventionModel = interventionModel;
}
}
I'm loading and setting InterventionModel
in a service class without problems as follows (intervention
instance exists):
private void loadInterventionModel(final Intervention intervention) {
final InterventionModel model = InterventionModel.findById(intervention.getLongId());
intervention.setInterventionModel(model);
}
The problem is that it does not work when I try to assess InterventionModel
attributes in the FreeMarker template:
"item_code:": ${intervention.intervention_model.imo_internal_code}
Here is the flushed error:
FreeMarker template error:
An error has occurred when reading existing sub-variable "intervention_model"; see cause exception! The type of the containing value was: extended_hash+string (app.models.Intervention wrapped into f.e.b.StringModel)
Caused by: java.lang.IllegalArgumentException: Attribute: 'intervention_model' is not defined in model: 'class app.models.Intervention.
What am I missing here and why it does not work as expected ? Generally, if I declare an attribute in the model with its accessors (getter and setter), will it be accessible in the template with:
mymodel.my_attribute