In many articles can see that if we want work many to many relations with extra colmuns the post/put requests must is done from Classroom or ClassType entities in several steps. Example:
curl -i -X POST -H "Content-Type:application/json"
-d "{\"name\":\"Room 1\"}" http://localhost:8080/Classrooms
curl -i -X POST -H "Content-Type:application/json"
-d "{\"name\":\"Aerobic\"}" http://localhost:8080/ClassTypes
curl -i -X PUT -H "Content-Type:text/uri-list"
--data-binary @uris.txt http://localhost:8080/Classrooms/1/ClassTypes
The uris.txt file contains the URIs of the ClassTypes, each on a separate line:
http://localhost:8080/ClassTypes/1
The problem is when i need do this in one transaction. To do this I need change focus and use as repository the Classroom/ClassType entity. In this case i could do this curl request:
curl -i -X POST -H "Content-Type:application/json" -d '{"id":{"classroom":"1","classType":"1"}, "classMax": 80}' http://localhost:8762/classroomproxy/classroomClassTypes/1_1
The JPA entity definition:
Classroom/ClassType
The repository
As the relation Classroom/ClassType (N to M) doesn't allow null, both for Classroom and for ClassType must load the information before saving in the database. If that is not done hibernate will generate an error saying "null reference". With Spring Data Rest we can handle the creation events. Before creating we can get the class information of entities relations and then set ClassroomClassType
Id Converter
To allow the customization of how entity ids are exposed in URIs generated.
Configuration
No hay comentarios:
Publicar un comentario