无法从@ElementCollection中删除

时间:2014-06-05 13:33:10

标签: java hibernate

在User.java中我有一张地图:

@Entity
public class User{
...
@ElementCollection
private Map<Competency,Float> competenciesValue = new HashMap<Competency,Float>();
}

并获得了能力等级

@Entity
public class Competency{
..
}

这是一对多关系。许多用户可以拥有很多能力。

现在,无论我在哪里尝试删除任何我的COMPETENCY,我都会收到外键错误。怎么处理呢?

Stacktrace:

Hibernate: delete from Competency where id=?
WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1451, SQLState: 23000
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot delete or update a parent row: a foreign key constraint fails (`CT2_szet`.`User_competenciesValue`, CONSTRAINT `FK_887tayi9xe3w6xw8jv2cui829` FOREIGN KEY (`competenciesValue_KEY`) REFERENCES `Competency` (`id`))
Jun 05, 2014 4:13:27 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/CompetenceTool] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`CT2_szet`.`User_competenciesValue`, CONSTRAINT `FK_887tayi9xe3w6xw8jv2cui829` FOREIGN KEY (`competenciesValue_KEY`) REFERENCES `Competency` (`id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)

1 个答案:

答案 0 :(得分:0)

以下是问题

Cannot delete or update a parent row: a foreign key constraint fails 
(CT2_szet.User_competenciesValue, CONSTRAINT FK_887tayi9xe3w6xw8jv2cui829 FOREIGN KEY (competenciesValue_KEY) REFERENCES Competency (id))

首先删除parent行,然后删除Comptency

中的相应行