简体   繁体   中英

Create relation / link one to one between 2 entities using spring data rest

Following this tutorial https://www.baeldung.com/spring-data-rest-relationships I'm trying to reproduce the link creation onetoone between 2 entities with a rest put call.

However I've got the following error message when I try to link addresses/1 & libraries/1/libraryAddress curl -i -X PUT -d " http://localhost:8080/addresses/1 " -H "Content-Type:text/uri-list" http://localhost:8080/libraries/1/libraryAddress

"Must send only 1 link to update a property reference that isn't a List or a Map"

Detail:

// 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> {}

queries made with the rest api:

create a library

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

create an adress

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

create the association

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

--> Error {"cause":null,"message":"Must send only 1 link to update a property reference that isn't a List or a Map."}

Have you got any clues to resolve this problem?

Regards, Blured.

It's actually a bug in Spring Boot 2.2.0 org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController class at line 321:

if (source.getLinks().hasSingleLink()) {
        throw new IllegalArgumentException(
            "Must send only 1 link to update a property reference that isn't a List or a Map.");
}

It should be:

if (!source.getLinks().hasSingleLink()) {
        throw new IllegalArgumentException(
            "Must send only 1 link to update a property reference that isn't a List or a Map.");
}

Here is the code for String Boot 2.1.7

if (source.getLinks().size() != 1) {
        throw new IllegalArgumentException(
            "Must send only 1 link to update a property reference that isn't a List or a Map.");
}

This kind of orders are compliant with 2.1.10.BUILD-SNAPSHOT but not with spring-boot 2.2.0.RELEASE

Things might have changed in this version. I will look at that. However thanks for having pointing that.

Somebody has already managed to run those kind of rest calls with 2.2.0?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM