我在Spring MVC Server中有2个JPA实体。其中一个是“父母”,另一个是孩子。 当我读到here(原因4)时,当我有父母和子实体时,首先必须删除父母,然后删除孩子。它正在制作一个org.hibernate.exception.ConstraintViolationException
家长实体项目
@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 }
儿童实体转发
@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; } }
我尝试在控制器
中的“@RequestMapping(value =”delete“)”中删除它们@RequestMapping(value = "delete", method = RequestMethod.GET) public ModelAndView DeletePostoSaude(Principal p,HttpServletResponse response) { if(p.getName().equals("coursera")) { Projects.deleteAll(); respostas.deleteAll(); } }
但 ConstraintViolationException 仍会出现:
Hibernate:选择project0_.id为id1_5_,project0_.date为date2_5_,project0_.listaperguntasjson为listaper3_5_,project0_.listarespostasjson为listares4_5_,project0_.nome_classe为nome_cla5_5_,project0_.project_name为project_6_5_来自Project project0_ Hibernate:从Project_Listaperguntas中删除Project_id =? Hibernate:从项目中删除id =? 2014-11-18 10:29:26.599 WARN 980 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:23503,SQLState:23503 2014-11-18 10:29:26.599 ERROR 980 --- [nio-8080-exec-2] ohengine.jdbc.spi.SqlExceptionHelper:ViolacióndeunalimitcióndeIntegridad Referencial:“FK_547Q6QW5LUAR2Q292SPD53J5G:PUBLIC.PROJECT_LISTARESPOSTAS FOREIGN KEY( PROJECT_ID)参考文献PUBLIC.PROJECT(ID)(193)“ 参照完整性约束违规:“FK_547Q6QW5LUAR2Q292SPD53J5G:PUBLIC.PROJECT_LISTARESPOSTAS FOREIGN KEY(PROJECT_ID)REFERENCES PUBLIC.PROJECT(ID)(193)”; SQL语句: 从项目中删除id =? [23503-175] 2014-11-18 10:29:26.599 INFO 980 --- [nio-8080-exec-2] o.h.e.j.b.internal.AbstractBatchImpl:HHH000010:在批量发布时它仍然包含JDBC语句 2014-11-18 10:29:26.601 ERROR 980 --- [nio-8080-exec-2] oaccC [。[。[/]。[dispatcherServlet]:servlet [dispatcherServlet]的Servlet.service()与上下文有关path []引发异常[请求处理失败;嵌套异常是org.springframework.dao.DataIntegrityViolationException:无法执行语句; SQL [不适用];约束[“FK_547Q6QW5LUAR2Q292SPD53J5G:PUBLIC.PROJECT_LISTARESPOSTAS FOREIGN KEY(PROJECT_ID)REFERENCES PUBLIC.PROJECT(ID)(193)”; SQL语句: 从项目中删除id =? [23503-175]];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句]的根本原因 org.h2.jdbc.JdbcSQLException:ViolacióndeunalimitcióndeIntegridad Referencial:“FK_547Q6QW5LUAR2Q292SPD53J5G:PUBLIC.PROJECT_LISTARESPOSTAS FOREIGN KEY(PROJECT_ID)REFERENCES PUBLIC.PROJECT(ID)(193)” 参照完整性约束违规:“FK_547Q6QW5LUAR2Q292SPD53J5G:PUBLIC.PROJECT_LISTARESPOSTAS FOREIGN KEY(PROJECT_ID)REFERENCES PUBLIC.PROJECT(ID)(193)”; SQL语句: 从项目中删除id =? [23503-175]
任何解决方案? (每个实体都有其CrudRepository)
答案 0 :(得分:1)
从错误来看,你的表似乎是相反的。我看到一个引用PROJECT的PROJECT_LISTARESPOSTAS。尝试使用相反的顺序删除:
respostas.deleteAll();
Projects.deleteAll();
您可能还想检查删除级联选项,以便在删除实体时也删除引用实体。这可能就像在引用另一个表的列上放置注释@OnDelete(action = OnDeleteAction.CASCADE)
一样简单(查看spring jpa文档以获取更多信息)