Alberto Crespo
Alberto Crespo

Reputation: 2509

deleting Child and Parent Entities on Spring MVC makes ConstraintViolationException

I have 2 JPA Entities in a Spring MVC Server. One of them is the "parent" and other is the Child. As I read here (Cause 4)that when I have an parent and child Entities, firstly a have to delete the parent and after delete the child. It is making a org.hibernate.exception.ConstraintViolationException

Parent Entity Project

@Entity
public class Project {  
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@JsonIgnore
private long id;
private String project_name;
private Date date;
private String nome_classe;
@ElementCollection(fetch=FetchType.EAGER)
List<respostas> Listarespostas = new ArrayList<respostas>(); //CHILD !!!!
public Project()
{}
//gets and sets
}

Child Entity Respostas

@Entity
public class respostas {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@JsonIgnore
private long id;
@ElementCollection(fetch=FetchType.EAGER)
List<String> lista = new ArrayList<String>();
String respostasjson;
public respostas()
{}
public respostas(List<String> lista)
{
    this.lista = lista;
    Gson gson = new Gson();
    gson.toJson(lista);
}
public void addResposta(String resposta)
{
    lista.add(resposta);
}
public void removeResposta(int i)
{
    lista.remove(i);
}
public String getRespostasjson() {
    return respostasjson;
}
public void setRespostasjson(String respostasjson) {
    this.respostasjson = respostasjson;
}
public List<String> getLista() {
    return lista;
}
}

I try to delete them in a "@RequestMapping(value = "delete")" in my Controller

 @RequestMapping(value = "delete", method = RequestMethod.GET)
public ModelAndView DeletePostoSaude(Principal p,HttpServletResponse response) 
{
    if(p.getName().equals("coursera"))
    {
        Projects.deleteAll();
        respostas.deleteAll();
    }
}

But the ConstraintViolationException still appears:

Hibernate: select project0_.id as id1_5_, project0_.date as date2_5_, project0_.listaperguntasjson as listaper3_5_, project0_.listarespostasjson as listares4_5_, project0_.nome_classe as nome_cla5_5_, project0_.project_name as project_6_5_ from Project project0_ Hibernate: delete from Project_Listaperguntas where Project_id=? Hibernate: delete from Project where id=? 2014-11-18 10:29:26.599 WARN 980 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 23503, SQLState: 23503 2014-11-18 10:29:26.599 ERROR 980 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Violación de una restricción de Integridad Referencial: "FK_547Q6QW5LUAR2Q292SPD53J5G: PUBLIC.PROJECT_LISTARESPOSTAS FOREIGN KEY(PROJECT_ID) REFERENCES PUBLIC.PROJECT(ID) (193)" Referential integrity constraint violation: "FK_547Q6QW5LUAR2Q292SPD53J5G: PUBLIC.PROJECT_LISTARESPOSTAS FOREIGN KEY(PROJECT_ID) REFERENCES PUBLIC.PROJECT(ID) (193)"; SQL statement: delete from Project where id=? [23503-175] 2014-11-18 10:29:26.599 INFO 980 --- [nio-8080-exec-2] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements 2014-11-18 10:29:26.601 ERROR 980 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FK_547Q6QW5LUAR2Q292SPD53J5G: PUBLIC.PROJECT_LISTARESPOSTAS FOREIGN KEY(PROJECT_ID) REFERENCES PUBLIC.PROJECT(ID) (193)"; SQL statement: delete from Project where id=? [23503-175]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause org.h2.jdbc.JdbcSQLException: Violación de una restricción de Integridad Referencial: "FK_547Q6QW5LUAR2Q292SPD53J5G: PUBLIC.PROJECT_LISTARESPOSTAS FOREIGN KEY(PROJECT_ID) REFERENCES PUBLIC.PROJECT(ID) (193)" Referential integrity constraint violation: "FK_547Q6QW5LUAR2Q292SPD53J5G: PUBLIC.PROJECT_LISTARESPOSTAS FOREIGN KEY(PROJECT_ID) REFERENCES PUBLIC.PROJECT(ID) (193)"; SQL statement: delete from Project where id=? [23503-175]

Any solution? (Each entity has its CrudRepository)

Upvotes: 0

Views: 1634

Answers (1)

Marios
Marios

Reputation: 1957

From the error it seems your tables work the other way around. I see a PROJECT_LISTARESPOSTAS referencing the PROJECT. Try deleting with opposite order:

respostas.deleteAll();
Projects.deleteAll();

you may also want to check the delete cascade option so that when deleting an entity, reference entities are deleted too. That could be as simple as putting an annotation @OnDelete(action = OnDeleteAction.CASCADE) on the column that references the other table (check the spring jpa documentation for more info)

Upvotes: 1

Related Questions