我使用多态关联将 2 个表链接到其公共表:
@BelongsToPolymorphic(parents = {Application.class, SiteSection.class})
public class Parameter extends Model {
static {
validatePresenceOf("parent_id", "parent_type");
}
}
控制器中使用的查询是这样的:
String siteSettingsParameters = SiteSection.where("site_id=?", site.getId()).include(Parameter.class).toJson(false);
结果 JSON 包含子节点:
[
{
"id": 253,
"section_id": 60,
"site_id": 61,
"children": {
"parameters": [
{
"created_at": "2017-12-19T15:50:28Z",
"id": 145,
"parent_id": 253,
"parent_type": "app.models.SiteSection",
"updated_at": "2017-12-19T15:50:28Z",
"value": "Terrible Swift Sword"
}
]
}
},
{
"id": 254,
"section_id": 61,
"site_id": 61,
"children": {
"parameters": [
{
"created_at": "2017-12-19T15:50:28Z",
"id": 146,
"parent_id": 254,
"parent_type": "app.models.SiteSection",
"updated_at": "2017-12-19T15:50:28Z",
"value": "Sleep the Brave"
}
]
}
},
...]
是否可以跳过Java 类中的子节点以映射到上述 JSON ?
这是我想映射 JSON 的 java 类的内容(我省略了访问器方法):
@JsonIgnoreProperties(ignoreUnknown = true)
public class SiteSectionDTO {
private Integer id;
private SectionDTO section;
private SiteDTO site;
@JsonProperty("children")
private List<ParameterDTO> parameters;
...
accessors come here
谢谢你。