4
I’m with a project using Spring Boot and Spring Data Rest to serve a Rest API.
When I’m serving an entity without relationships, it works smoothly.
The problem is when I use an entity with relationships. I cannot add new entities via POST. I tested with my Angularjs application and with Google Chrome extensions like Yet Another REST Client but the POST does not add the entity correctly. It edits an existing record causing confusion.
Follows the code:
@Entity
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private boolean capital;
@ManyToOne(targetEntity=State.class, fetch = FetchType.EAGER)
@JoinColumn(name = "state_id")
private State state;
// getters e setters omitidos
}
@RepositoryRestResource(excerptProjection = CityProjection.class)
public interface Cities extends PagingAndSortingRepository<City, Long>{
}
If necessary put more parts of the code. Still I don’t know what I did wrong or didn’t do to make it work properly.
All queries work correctly.
I did the following test, I sent a Request as POST with the following content:
{
"name": "Aparecida de Goiânia",
"capital": false,
"state": {
"id": "1",
"name": "Goiás",
"initials": "GO"
}
}
And I received as return and what appears in the database:
{
"id": 1,
"name": "Goiás",
"capital": false,
"_links": {
"self": {
"href": "http://localhost:8080/rest/cities/1"
},
"city": {
"href": "http://localhost:8080/rest/cities/1{?projection}",
"templated": true
},
"state": {
"href": "http://localhost:8080/rest/cities/1/state"
}
}
}
The same happens if I add "id": null
in the city.
What is the error and how the message is being sent to the service?
– Bruno César
No error is shown. But data is not entered.
– Shura16
Um, weird. Usually in these cases it is written log - according to the configured log level - that there is no mapped resource, that the received message is out of pattern, etc. Anyway, it puts at least the message you are testing, as said that you are editing an existing record, may be the message that is wrong (with id filled, for example).
– Bruno César
I changed the question with the requested return. In the log, nothing else appears. Only the Tomcat startup log (which is normal).
– Shura16
Dear, when you send in POST state_id instead of state and value 1, what happens? If you send the post without state records? What happens if you switch to Lazy? Have you run these tests? In the State class, the attributes are actually id, name and initials? In addition has access methods?
– Mateus
If I pass the field
state_id
or not pass, records, but the fieldstate_id
table is null. json also searches"state": null
. I did not test with Lazy because it is only the queries. And yes, entities have all public access methods (getters and setters).– Shura16
I also tested without Eager, the lack of it did not disturb the consultations and that is good, but still does not work the POST. also tested with unique field names with the
@Column
without success. I appreciate the help.– Shura16
I had a similar problem and used @Manytoone(Scade = Cascadetype.REFRESH) to avoid these "Updates". Put it there and see where it goes.
– Victor Magalhães
I ran the tests with
CascadeType.REFRESH
, but without success. Thanks for the help.– Shura16