这个让我难倒几个小时。这与我的另一个问题有关:
JPA/hibernate - Cannot add or update a child row: a foreign key constraint fails - BUT record exists
我摆脱了几乎所有代码并缩小了问题范围。我有三个非常简单的实体:
@Entity
public class Building {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
public Building() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Other.java
@Entity
public class Other {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name = "building_id")
private Building building;
public Other() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Building getBuilding() {
return building;
}
public void setBuilding(Building school) {
this.building = school;
}
}
Event.java
@Entity
public class Event {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@ManyToOne
@JoinColumn(name = "other_id")
private Other other;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Other getOther() {
return other;
}
public void setOther(Other other) {
this.other = other;
}
}
这将因外键约束违规而失败:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`blah`.`event`, CONSTRAINT `FK403827A76D0546B` FOREIGN KEY (`other_id`) REFERENCES `Other` (`id`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
现在这是真正的BIZARRE PART !!!!如果我做一个eclipse重构并重命名“其他”实体“Bob”,它的工作完全正常。就这样我们在同一页面上,Event实体现在看起来像这样:
@Entity
public class Event {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@ManyToOne
@JoinColumn(name = "bob_id")
private Bob bob;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Bob getBob() {
return bob;
}
public void setBob(Bob bob) {
this.bob = bob;
}
}
现在它完美无缺。有人可以向像我这样的Hibernate新手解释这个吗?这真让我难过。当我将新事件持久保存到服务层中的数据库时,它会失败:
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public void addEvent(Event event) throws ErrorCodeException, Exception
{
// not sure if I need to do this, still fails if I remove it
event.setOther(otherDAO.getReferenceById(event.getOther().getId()));
eventDAO.persist(event);
}
进一步评论。我也可以将表名设置为小写:
,而不是将其他名称重命名为Bob@Entity
@Table(name = "other")
public class Other {
此ALSO修复了它。但我无法接受这些解决方案。我想知道它失败的原因。我决定不使用Grails来避免这种“魔法”。
这是SQL hibernate日志:
// Initial database inserts
Hibernate: insert into Building values ( )
Hibernate: insert into Other (building_id) values (?)
// Insert which throws exception
Hibernate: insert into Event (other_id) values (?)
2011-06-27 11:31:51 JDBCExceptionReporter [WARN] SQL Error: 1452, SQLState: 23000
2011-06-27 11:31:51 JDBCExceptionReporter [ERROR] Cannot add or update a child row: a foreign key constraint fails (`blah`.`event`, CONSTRAINT `FK403827A76D0546B` FOREIGN KEY (`other_id`) REFERENCES `Other` (`id`))
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.blah.server.domain.Event]; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.blah.server.domain.Event]
答案 0 :(得分:2)
更新:我在这里找到了解决方案:
似乎mysql,hibernate和mac os x的组合导致了这个问题。作为解决方案,请使用小写命名策略。