spring - How to update bidirectional @ManyToOne/@OneToMany relationship via REST from the inverse side -
i have 2 entities in spring data rest app:
@entity public class question { ... @onetomany(mappedby = "question") private set<answer> answers = new hashset<>(); ... } @entity public class answer { ... @manytoone question question; ... }
and spring data repositories both entities:
@repositoryrestresource public interface questionrepository extends pagingandsortingrepository<question, long> { } @repositoryrestresource public interface answerrepository extends pagingandsortingrepository<answer, long> { }
suppose have question id 1 , 2 answers ids 1 , 2. able link them this:
curl -v -x put -h "content-type: text/uri-list" -d "http://localhost:8080/questions/1" http://localhost:8080/answers/1/question curl -v -x put -h "content-type: text/uri-list" -d "http://localhost:8080/questions/1" http://localhost:8080/answers/2/question
but not this:
curl -v -x put -h "content-type:text/uri-list" -d $'http://localhost:8080/answers/1\nhttp://localhost:8080/answers/2' http://localhost:8080/questions/1/answers
spring data rest processes last curl request fetching question instance id 1, adding 2 answers set , invoking hibernate merge on question.
the problem changes ignored since made inverse (not owner of relationship) side. , far know cannot change owner or make updateable both sides in one-to-many/many-to-one relationship in hibernate.
does mean have update answers set one-by-one? possible configure spring data rest update relation owners side gets persisted hibernate?
i new spring data rest.the reason (i think) spring data rest not following:
a put request object means should updated have give (e.g 2 answers question). however, if later decide put 2 different answer objects on uri list, should previous 2 answer objects point ? should set null ? if model said @notnull question field in answer entity?
in summary, updating answer point different question defined updating question's answer list not defined spring data rest avoids this. of course estimated guess , haven't looked @ code behind spring data rest
Comments
Post a Comment