使用旧数据库时,有时我无法使用belongs_to
注释正确设置关系。在这种情况下,我尝试使用其访问器方法定义指向另一个Model
类的属性,如下所示:
@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;
}
}
我InterventionModel
在服务类中加载和设置没有问题如下(intervention
实例存在):
private void loadInterventionModel(final Intervention intervention) {
final InterventionModel model = InterventionModel.findById(intervention.getLongId());
intervention.setInterventionModel(model);
}
InterventionModel
问题是当我尝试评估FreeMarker 模板中的属性时它不起作用:
"item_code:": ${intervention.intervention_model.imo_internal_code}
这是刷新的错误:
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.
我在这里缺少什么以及为什么它不能按预期工作?一般来说,如果我在模型中声明一个属性及其访问器(getter 和 setter),它是否可以在模板中访问:
mymodel.my_attribute