使用spring数据休止符在2个实体之间创建关系/链接到一对一

问题描述 投票:0回答:1

根据本教程https://www.baeldung.com/spring-data-rest-relationships,我试图通过一个剩余放置调用来重现两个实体之间的链接创建。

但是,当我尝试链接地址/ 1和库/ 1 / libraryAddress时,出现以下错误消息curl -i -X PUT -d“ http://localhost:8080/addresses/1”-H“内容类型:文本/ uri-list” http://localhost:8080/libraries/1/libraryAddress

“必须仅发送1个链接来更新不是列表或地图的属性引用”

详细:

// Data model
// Master library class which have one address
@Entity
public class Library {

    @Id
    @GeneratedValue
    private long id;

    @Column
    private String name;

    @OneToOne
    @JoinColumn(name = "address_id")
    @RestResource(path = "libraryAddress", rel="address")
    private Address address;

    // standard constructor, getters, setters
}

// Address linked with a onetoone relation with library
@Entity
public class Address {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false)
    private String location;

    @OneToOne(mappedBy = "address")
    private Library library;

    // standard constructor, getters, setters
}

// Repositories
public interface LibraryRepository extends CrudRepository<Library, Long> {}
public interface AddressRepository extends CrudRepository<Address, Long> {}

使用其余api进行的查询:

创建图书馆

curl -i -X POST -H "Content-Type:application/json"
  -d '{"name":"My Library"}' http://localhost:8080/libraries

创建地址

curl -i -X POST -H "Content-Type:application/json"
  -d '{"location":"Main Street nr 5"}' http://localhost:8080/addresses

创建关联

curl -i -X PUT -d "http://localhost:8080/addresses/1"
  -H "Content-Type:text/uri-list" http://localhost:8080/libraries/1/libraryAddress

->错误{“ cause”:null,“ message”:“必须仅发送1个链接来更新不是列表或地图的属性引用。”}

您是否有任何解决此问题的线索?

关于,模糊。

rest spring-boot jpa spring-data-rest hateoas
1个回答
0
投票

此类订单符合2.1.10.BUILD-SNAPSHOT的要求,但不适用于spring-boot 2.2.0.RELEASE

此版本中的内容应该已更改。我会看的。但是,感谢您指出这一点。

有人已经设法在2.2.0下运行这种类型的Rest调用?

© www.soinside.com 2019 - 2024. All rights reserved.