JPA一对多关系在更新

时间:2017-04-26 14:08:49

标签: hibernate jpa spring-data-jpa hibernate-mapping

我有以下实体。问题是,当我从FormComponent中删除FormComponentOption子项并且我在Form上调用更新时,子项不会从数据库中删除。我希望当我更新表单时,我已从FormComponentOption集合中删除了一个子项,必须从FormComponentOption表中删除此子项。我已经在FormComponent表和FormComponentOption表之间的OneToMany关系上放了orphanRemoval = true但是不起作用。可能是什么问题?

@Data
@Entity
@Table(name = "form")
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Form implements Serializable {

   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(generator = "system-uuid")
   @GenericGenerator(name = "system-uuid", strategy = "uuid")
   private String id;

   private String name;

   @OneToMany(mappedBy = "form", cascade = { CascadeType.PERSIST, 
                     CascadeType.MERGE }, orphanRemoval = true)
   @NotFound(action = NotFoundAction.IGNORE)
   @Singular
   private List<FormComponentJoin> formComponentJoin = new ArrayList<>();

}

@Data
@EqualsAndHashCode
@Entity
@Table(name = "form_component")
@AllArgsConstructor(suppressConstructorProperties = true)
@NoArgsConstructor
public class FormComponent implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    @JsonIgnore
    @OneToMany(mappedBy = "formComponent", cascade = { CascadeType.PERSIST, 
CascadeType.MERGE }, orphanRemoval = true,             
           orphanRemoval = true)
    @NotFound(action = NotFoundAction.IGNORE)
    private List<FormComponentJoin> formComponentJoin = new ArrayList<>();

    @ManyToOne
    @JsonIgnore
    private FormComponent parent;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    @Singular
    private List<FormComponent> children;

    @OneToMany(mappedBy = "formComponent", cascade = 
        CascadeType.ALL,orphanRemoval=true)
    @Singular
    private List<FormComponentOption> options;

    private String value;
}

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "form_component_option")
@Builder
@AllArgsConstructor(suppressConstructorProperties = true)
@NoArgsConstructor
@JsonPropertyOrder(value = { "id", "customId", "key", "labels" })
public class FormComponentOption extends AbstractEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "form_component_id")
    @JsonIgnore
    private FormComponent formComponent;

    private String key;
}

@Entity
@Table(name = "form_component_join")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class FormComponentJoin implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    @ManyToOne
    @JoinColumn(name = "form_id", referencedColumnName = "id")
    @NotFound(action = NotFoundAction.IGNORE)
    @JsonIgnore
    private Form form;

    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinColumn(name = "form_component_id", referencedColumnName = "id")
    @NotFound(action = NotFoundAction.IGNORE)
    private FormComponent formComponent;

    @Column(name = "position")
    private int pos;

}

@Repository
@Transactional(readOnly = true)
public interface FormRepository extends PagingAndSortingRepository<Form, 
    String> {

}

这是我得到的错误: 引起:org.postgresql.util.PSQLException:错误:更新或删除表&#34; form_component&#34;违反外键约束&#34; fk_jr7qfhx86ga4l5ewok6810nqx&#34;在表格&#34; form_component_join&#34;   细节:Key(id)=(284aa4fb5baaa81c015baaa8397c0002)仍然从表&#34; form_component_join&#34;中引用。

但是我删除了FormComponent的FormComponentOption子节点而不是FormComponent。

这是我更新整个表单的方式:

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/forms")
public class FormRestControllerImpl extends AbstractRestControllerImpl<Form, 
    DTO> {

    @Autowired
    public FormRestControllerImpl(CrudController<Form, DTO> 
         formCrudController) {
        this.controller = formCrudController;
    }

    @RequestMapping(method = RequestMethod.POST, consumes = 
    MediaType.APPLICATION_JSON_VALUE, produces = 
    MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "Create a NEW element. If an ID is specified, it 
    may be ignored by the system in favour of an auto-generated one. Return 
    the 
    created entity.", httpMethod = "POST", response = ResponseDTO.class)
    @JsonView(EntityView.SummaryView.class)
    @Override
    public ResponseDTO create(@RequestBody Form form) {
        FormUtil.setFormChildren(form);
        return prepareResponseDtoFromEntity(((CrudController) 
        controller).create(form));
    }


   @CrossOrigin // TODO set the address of the web application
   @RequestMapping(method = RequestMethod.PUT, consumes = 
   MediaType.APPLICATION_JSON_VALUE, produces = 
   MediaType.APPLICATION_JSON_VALUE)
   @ApiOperation(value = "Update an EXISTING element. Return the updated 
   entity", httpMethod = "PUT", response = ResponseDTO.class)
   @Override
   public ResponseDTO update(@RequestBody Form form) {
       FormUtil.setFormChildren(form);
       return prepareResponseDtoFromEntity(((CrudController) 
       controller).update(form));
    }
}

0 个答案:

没有答案