I have a REST Web Service API. I mapped database using JPA. I have an entity “persona”. Adding a new entity with POST method works fine, also GET method works fine, but when I try to call PUT method, there is some exception that I found out while debugging: “Cannot suppress a null exception.” and “Self-suppression not permitted”. In the test database with other entities everything works fine…
Adding works almost the same like editing in my case – firstly I only get the values and then after changing I do everything the same, the only difference is that there is used method PUT instead of POST.
This is my PUT method (here the exception occurs):
@PUT
@Consumes({"application/xml", "application/json"})
public Response edit(Persona entity) {
try {
getJpaController().edit(entity);
return Response.ok().build();
} catch (Exception ex) {
return Response.notModified(ex.getMessage()).build();
}
}
I’m working first time with web services, so I’m a newbie.
What can be the reason of such behaviour? What is this self-suppression error?
If you need any source code else, please, tell me, I will edit my post.
@EDIT:
I found some exception in JpaController Class, my Persona class has:
@OneToOne(cascade = CascadeType.ALL, mappedBy = "persona")
private Personaacceso personaacceso;
In JpaController there is:
Personaacceso personaaccesoOld = persistentPersona.getPersonaacceso();
Personaacceso personaaccesoNew = persona.getPersonaacceso();
if (personaaccesoOld != null && !personaaccesoOld.equals(personaaccesoNew)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Personaacceso " + personaaccesoOld + " since its persona field is not nullable.");
}
So there is showing a message that I have to retain Personaacceso. Any idea how to solve it?
Okey, I know what was the reason now…
Each Persona entity has some other entities (one-to-one or one-to-many). The Jpa Controller wasn’t retaining all these containing entities (instead, it was creating new ones). So, the data was lost and because of that persona entity wasn’t edited.
Solution:
To every containing entity do something like:
However, take into consideration, that then these entity-fields are just rewrited from the old “main” entity.