简体   繁体   中英

PUT on ManyToMany association in Spring Data Rest

I'm trying to figure out how to PUT directly on a ManyToMany association I have.

Example of my entities (names changed for extra confusion):

First entity:

@Entity
public class First {
    @Id
    private Long id;

    private String name;

    @OneToMany(mappedBy = "first")
    private Set<Third> thirds = new HashSet<>();
}

Second entity:

@Entity
public class Second {
    @Id
    private Long id;

    private String name;

    @OneToMany(mappedBy = "second")
    private Set<Third> thirds = new HashSet<>();
}

Third entity:

@Entity
public class Third {
    @Id
    private Long id;

    private String type;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "first_id")
    private First first;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "second_id")
    private Second second;
}

Now what I want to do is something like this:

PUT /first/1/thirds
{
    "second": "/second/1",
    "type": "TEST"
}

But nothing happens. Is it possible to PUT associations this way when you use an entity in the middle of a ManyToMany? Or should I just POST the association directly to /third?

you first have to create a First instance, like this:

curl -i -X POST -d "{\"name\":\"first\"}"
  -H "Content-Type:application/json" http://localhost:8080/firsts

then you have to create a Third instance:

curl -i -X POST -H "Content-Type:application/json"
  -d '{\"type\":\"third\"}' http://localhost:8080/thirds

and eventually, you can create an association with PUT:

curl -i -X PUT -H "Content-Type:text/uri-list"
-d "http://localhost:8080/firsts/1" http://localhost:8080/thirds/1/first

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