我使用spring框架和hibernate作为ORM工具。
我的父类是这样的:
@Entity
public class Category {
@Id
@GeneratedValue
@NotNull
private int cid;
private String cname;
@OneToMany(cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name = "cid")
List<Ad> ads;//getter和setter,构造函数}
我的孩子类是这样的:
@Entity
public class Ad {
private int adid;
private String adName;
//getter and setter, constructor
}我的类别控制器是:
@Controller
public class CategoryController {
@Autowired
SessionFactory sessionFactory;
Session session;
@Transactional
@RequestMapping(value = "categories",method = RequestMethod.GET)
@ResponseBody
public List<Category> getAllCategory()throws SQLException{
session=sessionFactory.getCurrentSession();
return session.createCriteria(Category.class).list();
}
}当我点击url localhost:8080/categories .I会得到如下的json数据:
{"cid":"1",cname":"category","ads":[{"adid":"1","adName":"ad"}]}这里我同时获取了父table.But和相关子table.Here的数据,如何才能只获取父table.Here的数据在本例中,我需要的数据如下:
{"cid":"1",cname":"category"}我该怎么做呢?
发布于 2017-03-10 18:25:32
我看到一篇很好的文章,它准确地描述了你的问题。
Json Exclude Property
通过使用@JsonIgnore和@JsonProperty注释配置实体,您可以实现这一点。
发布于 2017-03-10 17:59:05
https://stackoverflow.com/questions/42715300
复制相似问题