与Spring Data Rest关联的错误POST和PATCH处理

时间:2016-06-23 09:51:38

标签: spring spring-data-rest

Spring Data Rest在更新嵌入式关联时显示出一种真正令人费解的行为。在我的示例中,有一个实体 Customer ,它与实体 Phones 具有_OneTo_Many_关系。通过 GET 将电话正确显示为嵌入式阵列到 customers / 50 实体资源。但 PUT PATCH 都没有显示预期的结果。

public class Customer {
    public static final String TABLE_NAME = "CUSTOMER";
    public static final String SEQUENCE_NAME = "S_CUSTOMER";
    public static final String DISPLAY_NAME_COLUMN = "DISPLAY_NAME";

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE,
            generator = "s_customer")
    @SequenceGenerator(name = "s_customer", sequenceName = "S_CUSTOMER",
            allocationSize = 50)
    private Long id;

    @NotEmpty
    @Column(name = "DISPLAY_NAME")
    private String displayName;

    @OneToMany(mappedBy = "owner")
    private List<Phone> phones;

    @Version
    private Long version;
}

public class Phone {
    public static final String TABLE_NAME = "PHONE";
    public static final String SEQUENCE_NAME = "S_PHONE";
    public static final String OWNER_COLUMN = "OWNER";
    public static final String PHONE_TYPE_COLUMN = "PHONE_TYPE";
    public static final String NUMBER_COLUMN = "NUMBER";

    enum PhoneType {
        MOBILE, HOME, OFFICE
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "s_phone_number")
    @SequenceGenerator(name = "s_phone_number", sequenceName = SEQUENCE_NAME, allocationSize = 50)
    private Long id;

    @ManyToOne
    @JoinColumn(name = OWNER_COLUMN)
    private Customer owner;

    @Enumerated(EnumType.STRING)
    @Column(name = PHONE_TYPE_COLUMN)
    private PhoneType phoneType;

    @NotEmpty
    @Column(name = NUMBER_COLUMN)
    private String number;

    @Version
    private Long version;
}

POST 到实体/ customer / 50会为客户生成正确的更新,但插入 > 电话不包含客户的外键:

  

[EL精细]:sql:2016-06-23   11:41:25.149 - ClientSession的(1317378011) - 连接(497905718) - 线程(线程[HTTP-NIO-8081-EXEC -1,5,主]) - UPDATE   CUSTOMER SET DISPLAY_NAME = ?, VERSION =? WHERE((ID =?)AND   (VERSION =?))bind =&gt; [bla,1,50,0] [EL精细]:sql:2016-06-23   11:41:25.15 - ClientSession的(1317378011) - 连接(497905718) - 线程(线程[HTTP-NIO-8081-EXEC -1,5,主]) - INSERT   INTO PHONE(ID,NUMBER,PHONE_TYPE,VERSION,OWNER)值(?,?,?,   ?,?)bind =&gt; [1,12345,MOBILE,1,null]

这是 PUT 请求的正文:

{
    "displayName": "bla",
    "phones": [
        {
            "number": "12345",
            "phoneType": "MOBILE"
        }
    ]
}

因此,Spring Data Rest正确地将 PUT 解释为Customer的更新和插入Phone表,但只是&#34;忘记&#34;关于这种关系。我认为这是一个错误。或者有什么我忘了?

Btw: PATCH 的行为相似。电话记录中再没有外键。

修改

CustomerRepository的代码:

@Repository
public interface CustomerDao extends PagingAndSortingRepository<Customer, Long> {
    List<Customer> findByDisplayName(@Param("name") String name);
}

0 个答案:

没有答案