Java一对多持久化hashset

时间:2017-09-08 20:27:23

标签: java hibernate jpa java-ee

有人可以指导我当我从邮递员发送请求时,我收到reportFilter为空。 '名称'字段已正确设置但是报告过滤器为空。我在OneToMany上缺少任何注释?我正在调用/ api / save,它接受ReportTemplate对象,但我没有在该对象中获取reportFilter List

@Entity
@Audited
@XmlRootElement
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "name"))
@JsonIgnoreProperties(value = { "handler", "hibernateLazyInitializer" })
public class ReportTemplate {
 private String name;
 private String templateFormat;
 private Set<ReportFilter> reportFilter = new HashSet<>();

 @NotNull
 public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

 @OneToMany(mappedBy="reportTemplate")
 @JsonIgnore
 public Set<ReportFilter> getReportFilter() {
    return reportFilter;
 }

  public void setReportFilter(Set<ReportFilter> reportFilter) {
    this.reportFilter = reportFilter;
  }
}

@Entity
@Audited
@XmlRootElement
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"column_1", "reportTemplate_id"}))
@JsonIgnoreProperties(value = { "handler", "hibernateLazyInitializer" })
public class ReportFilter {

private String column_1;
private ReportTemplate reportTemplate;

 @NotNull
 public String getColumn_1() {
    return column_1;
 }

 public void setColumn_1(String column_1) {
    this.column_1 = column_1;
 }

 @ManyToOne
 @NotNull
 public ReportTemplate getReportTemplate() {
    return reportTemplate;
 }

 public void setReportTemplate(ReportTemplate reportTemplate) {
    this.reportTemplate = reportTemplate;
 }
}

邮差要求:

{
    "name": "Test-Report",
    "reportFilter": [
        {
            "filterColumn": "Reference Number",
            "column_1": "A",
            "columnValue": "HE222",
        },
        {
            "filterColumn": "Test Column",
            "column_1": "B",
            "columnValue": "1234",
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

通过从这段代码中删除@JsonIgone来解决您的问题:

@OneToMany(mappedBy="reportTemplate")
public Set<ReportFilter> getReportFilter() {
   return reportFilter;
}

使用这个anntotaion,你说JsonEncoderDecoder在从json对象转换为hibernate实体时忽略了这个属性。

你也错过了@JoinColumn两个人之间关系的一方。使用这样的东西:

@ManyToOne
@JoinColumn(name = "TEMPLATE_ID")
@NotNull
public ReportTemplate getReportTemplate() {
    return reportTemplate;
}

您需要这个来定义所有者实体。

并且所有实体MOST都有一个@Id列用于主键。