简体   繁体   中英

Using Spring-Data-Rest, how to update FK on both sides of OneToOne relationship from one REST call?

I have a Spring-Boot app with

  • spring-boot-starter-data-rest
  • spring-boot-starter-data-jpa
  • h2

I have entities with one-to-one relationship like this:

@Entity
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long key;

    @OneToOne(cascade = ALL)
    @JoinColumn(name = "party")
    private Party party;

    @Column
    private String street;

    ....
}
@Entity
public class Party {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long key;

    @Column
    private String name;

    @OneToOne(cascade = ALL)
    @JoinColumn(name  = "address")
    private Address address;

    ...

}

And repositories for each entity:

@RepositoryRestResource
public interface AddressRepository extends JpaRepository<Address, Long> { }
@RepositoryRestResource
public interface PartyRepository extends JpaRepository<Party, Long> { }

I create an instance of each entity:

curl -X POST \
     -H 'Content-Type: application/json' \
     -d '{ "name": "John Smith" }' \
     http://localhost:8080/parties
curl -X POST \
     -H 'Content-Type: application/json' \
     -d '{ "street": "456 main st" }' \
     http://localhost:8080/addresses

On the address instance, I create a HATEOAS association to party:

curl -X PUT \
     -H 'Content-Type: text/uri-list' \
     -d http://localhost:8080/parties/1 \
     http://localhost:8080/addresses/1/party

When I check the associations of address:

curl -X GET http://localhost:8080/addresses/1/party -i

I see the correct party:

HTTP/1.1 200 
{
  "key" : 1,
  "name" : "John Smith",
  ....
}

But, when I check the associations of address:

curl -X GET http://localhost:8080/parties/1/address -i

It does not exist:

HTTP/1.1 404 

How do I create both associations from a single call using Spring-Data-Rest?

you need to execute your curl like below

curl -X POST \
     -H 'Content-Type: application/json' \
     -d '{ "name": "John Smith" , "address":{ "street": "456 main st" }}' \
     http://localhost:8080/parties

There are some minor changes I did in Entity

@Entity
public class Party implements Serializable{


    private static final long serialVersionUID = 1735292011581348691L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long key;

    @Column
    @JsonProperty("name")
    private String name;

    @JsonProperty("address")
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name  = "address")
    private Address address;


}

@Entity
public class Address implements Serializable {
private static final long serialVersionUID = -7468012139122259209L;


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long key;


    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "party")
    private Party party;

    @JsonProperty("street")
    @Column
    private String street;

}

Works fine, when I tested. it's saving in both table

curl to get

curl -X  http://localhost:8080/parties/1

I found a solution for this using @PrePersist and @PreUpdate on my Party class:

@Entity
public class Party {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long key;

    @Column
    private String name;

    @OneToOne(cascade = ALL)
    @JoinColumn(name  = "address")
    private Address address;

    @PrePersist
    @PreUpdate
    public void updateAddressAssociation() {
        if (address != null)
            address.setParty(this);
    }

}

Here is my app flow:

  1. Create the address:
curl -X POST \
     -H 'Content-Type: application/json' \
     -d '{ "street": "456 main st" }' \
     http://localhost:8080/addresses
  1. Create the party
curl -X POST \
     -H 'Content-Type: application/json' \
     -d '{ "name": "John Smith" }' \
     http://localhost:8080/parties
  1. Associate address with party
curl -X PUT \
     -H 'Content-Type: text/uri-list' \
     -d http://localhost:8080/parties/1 \
     http://localhost:8080/addresses/1/party
  1. Confirm party has address association
curl http://localhost:8080/parties/1/address
  1. Confirm address has party association
curl http://localhost:8080/addresses/1/party

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