我正在使用Spring MVC + Hibernate + Restful Web服务+ AngularJs来创建一个Web应用程序, 由于无限包含,我对实体有问题,我用@JsonManagedReference和@JsonBackReference解决了它。 现在我不再拥有无限循环,但Json返回客户端并不是预期的,缺少两个属性,并且我没有错误消息
数据库关系:
返回的Sim对象是
{
'id' : ,
'scriptList' : [...]
}
而不是
{
'id' : ,
'scriptList' : [ ... ] ,
'operator' : ,
'package' :
}
...
@Entity
@Table(name = "sim")
public class Sim {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "IDSIM")
private Integer idsim;
@JsonManagedReference("sim-script)
@OneToMany(cascade = CascadeType.ALL, mappedBy = "sim")
private List<Script> scriptList=new ArrayList<>();
@JsonBackReference("package-sim")
@ManyToOne(optional = false)
@JoinColumn(name = "IDPACKAGE", referencedColumnName = "IDPACKAGE")
private Offre offre;
@ManyToOne(optional = false)
@JoinColumn(name = "IDOPERATOR", referencedColumnName = "IDOPERATOR")
@JsonBackReference("operator-sim")
private Operator operator;
public Sim() {
}
public Sim(Integer idsim) {
this.idsim = idsim;
}
public Integer getIdsim() {
return idsim;
}
public void setIdsim(Integer idsim) {
this.idsim = idsim;
}
public List<Script> getScriptList() {
return scriptList;
}
public void setScriptList(List<Script> scList) {
this.scriptList = scriptList;
}
public Package getPackage() {
return package;
}
public void setPackage(Package Package) {
this.offre = offre;
}
public Operator getOperator() {
return operator;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
}
...
@Entity
@Table(name = "Operator")
public class Operateur {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "IDOPERATOR")
private Integer idoperator;
...
@JoinTable(name = "operator_package", joinColumns = {
@JoinColumn(name = "IDOPERATOR", referencedColumnName = "IDOPERATOR")}, inverseJoinColumns = {
@JoinColumn(name = "IDPACKAGE", referencedColumnName = "IDPACKAGE")})
@ManyToMany
private List<Package> packageList=new ArrayList<>();
@JsonManagedReference("operator-sim")
@OneToMany(cascade = CascadeType.ALL, mappedBy = "operator")
private List<Sim> simList =new ArrayList<>();
public Operator() {
}
...
public List<Operator> getOperatorList() {
return operatorList;
}
public void setPackageList(List<Package> packageList) {
this.packageList = packageList;
}
public List<Sim> getSimList() {
return simList;
}
public void setSimList(List<Sim> simList) {
this.simList = simList;
}
}
...
@Entity
@Table(name = "package")
public class Offre {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "IDPACKAGE")
private Integer idoffre;
...
@ManyToMany(mappedBy = "packageList")
private List<Operator> operatorList;
@JsonManagedReference("package-sim")
@OneToMany(cascade = CascadeType.ALL, mappedBy = "package")
private List<Sim> simList=new ArrayList<>();
public Package() {
}
...
public List<Operator> getOperatorList() {
return operatorList;
}
public void setOperatorList(List<Operator> operatorList) {
this.operatorList = operatorList;
}
public List<Sim> getSimList() {
return simList;
}
public void setSimList(List<Sim> simList) {
this.simList = simList;
}
}