如何使用Spring Data JPA更新子列表?

时间:2016-12-20 07:27:28

标签: java jpa spring-data

我有一个编辑非页面,用户可以添加或删除NotRecipient。

问题是当我保存已编辑的Not对象时,它将已编辑的NotRecipients保存为新的。我想要做的是当我保存Not object check NotRecipients name字段来更新或添加新的。有没有办法做到这一点?

@Entity
@Table(name = "not")
public class Not implements Serializable{

  @Id
  @Column(name = "not_id")
  @GeneratedValue(generator = "system-uuid")
  @GenericGenerator(name = "system-uuid", strategy = "org.hibernate.id.UUIDGenerator")
  private String notId;

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "not", fetch = FetchType.EAGER)
  private List<NotRecipient> recipients = new ArrayList<>();

  //getters and setters
}


@Entity
@Table(name = "not_reciepent")
public class NotRecipient implements Serializable {
  @Id
  @Column(name = "not_recipient_id")
  @GeneratedValue(generator = "system-uuid")
  @GenericGenerator(name = "system-uuid", strategy = "org.hibernate.id.UUIDGenerator")
  private String notRecipientId;

  @JsonIgnore
  @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name = "not_id")
  private Not not;

  @Column
  private String name;
  //getters and setters
}

0 个答案:

没有答案