我有一个Category
域模型对象,正在为Angular应用构建一个REST风格的后端接口。目前,Category
对象创建和持久性按预期工作,但categoryList
的内部属性在所有GET请求上返回null。
我的Category
型号:
@Entity
@NamedQuery(
name="findCategoryByName",
query="select category FROM Category c WHERE c.category = :category"
)
@Table(name = "categories")
public class Category implements Serializable{
@Id
@Column(name = "category_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column
private String category;
@ManyToMany(mappedBy = "categories", cascade = CascadeType.PERSIST)
private List<Photo> photos;
@ManyToOne(cascade = CascadeType.PERSIST)
private Category parent;
@OneToMany(mappedBy = "parent")
@JsonDeserialize(as = ArrayList.class, contentAs = Category.class)
private List<Category> categoryList;
public Category() { }
}
AppConfig.java
中我的杰克逊配置:
public MappingJackson2HttpMessageConverter jacksonMessageConverter(){
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Hibernate4Module());
converter.setObjectMapper(mapper);
return converter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converterList){
converterList.add(jacksonMessageConverter());
super.configureMessageConverters(converterList);
}
我的CategoryRepository
实施中通过此方法检索数据:
@Override
public List<Category> findAll(){
List<Category> results = getEntityManager().createQuery("from Category").getResultList();
return results;
}
当我向localhost:8080/admin/categories
发送GET请求时,我得到了
{
"id": 42,
"category": "newCategory",
"photos": null,
"parent": {
"id": 41,
"category": "testCategory",
"photos": null,
"parent": null,
"categoryList": null
},
"categoryList": null
}
但是,如果我在return results;
方法实现中的FindAll()
上设置了断点,我可以看到Category
父categoryList
字段的长度为1并包含对newCategory
类别的引用。
为什么此列表值不会出现在GET请求中?