如何使用带有Spring Data REST的JSONPatch将元素正确添加到集合中?

Gab*_*man 5 spring spring-data spring-data-jpa spring-data-rest

我有一个非常简单的Spring Data REST项目,它有两个实体,Account和AccountEmail.有一个帐户存储库,但不适用于AccountEmail.帐户与AccountEmail有@OneToMany关系,并且AccountEmail没有反向链接.

更新:我认为这是一个错误.在Spring JIRA上被命名DATAREST-781.我包括了一个演示项目和复制说明.

我可以使用以下调用创建一个帐户:

$ curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"emails":[{"address":"nil@nil.nil"}]}' \ 
  http://localhost:8080/accounts
Run Code Online (Sandbox Code Playgroud)

哪个回报:

{
  "emails" : [ {
    "address" : "nil@nil.nil",
    "createdAt" : "2016-03-02T19:27:24.631+0000"
  } ],
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/accounts/1"
    },
    "account" : {
      "href" : "http://localhost:8080/accounts/1"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用JSONPatch为该帐户添加另一个电子邮件地址:

$ curl -X PATCH \
   -H "Content-Type: application/json-patch+json" \
   -d '[{ "op": "add", "path": "/emails/-","value":{"address":"foo@foo.foo"}}]' \
   http://localhost:8080/accounts/1
Run Code Online (Sandbox Code Playgroud)

这会向集合中添加一个新对象,但由于某种原因,该地址为null:

{
  "emails" : [ {
    "address" : null,
    "createdAt" : "2016-03-02T19:30:06.417+0000"
  }, {
    "address" : "nil@nil.nil",
    "createdAt" : "2016-03-02T19:27:24.631+0000"
  } ],
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/accounts/1"
    },
    "account" : {
      "href" : "http://localhost:8080/accounts/1"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么新添加的对象的地址为null?我错了吗?任何提示赞赏.

我正在使用Spring Boot 1.3.2.RELEASE和Spring Data Gosling-SR4.支持数据库是HSQL.

以下是有问题的实体:

@Entity
public class Account {

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(cascade = CascadeType.PERSIST)
    private List<AccountEmail> emails = Lists.newArrayList();

}

@Entity
public class AccountEmail {

    @Id
    @GeneratedValue
    private Long id;

    @Basic
    @MatchesPattern(Regexes.EMAIL_ADDRESS)
    private String address;

    @CreatedDate
    @ReadOnlyProperty
    @Basic(optional = false)
    @Column(updatable = false)
    private Date createdAt;

    @PrePersist
    public void prePersist() {
        setCreatedAt(Date.from(Instant.now()));
    }

}
Run Code Online (Sandbox Code Playgroud)