Java spring框架下hibernate一对多关系中如何仅从父类获取json数据
我使用spring框架和hibernate作为ORM工具 我的父类是:Java spring框架下hibernate一对多关系中如何仅从父类获取json数据,java,json,spring,hibernate,rest,Java,Json,Spring,Hibernate,Rest,我使用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
@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;
@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();
}
}
在这里,我同时获取父表和相关子表的数据。但是,我如何才能只获取父表的数据呢
例如,我需要以下数据:
{"cid":"1",cname":"category","ads":[{"adid":"1","adName":"ad"}]}
{"cid":"1",cname":"category"}
我如何才能做到这一点您可以尝试以下方法
基本上必须在需要断开链接的地方应用排除我看到了一篇很好的文章,它准确地描述了您的问题 通过使用
@JsonIgnore
和@JsonProperty
注释配置实体,您可以实现这一点