0
這個錯誤讓我當我嘗試使用Hibernate插入(保存)的用戶:休眠:不能添加或更新子行,外鍵約束失敗
//SQL
DROP TABLE IF EXISTS `bytecodete`.`account_confirmation`;
CREATE TABLE `bytecodete`.`account_confirmation` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
KEY `FK_account_confirmation_1` (`email`),
CONSTRAINT `FK_account_confirmation_1` FOREIGN KEY (`email`) REFERENCES `user` (`email`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
// HIBERNATE
@Entity
@Table(name = "account_confirmation", catalog = "bytecodete")
public class AccountConfirmation implements java.io.Serializable {
private Integer id;
private User user;
public AccountConfirmation() {
}
public AccountConfirmation(User user) {
this.user = user;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "email", nullable = false)
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
我第一次插入對象「用戶」在數據庫中,然後我嘗試插入這個用戶在這個表'account_confirmation',但是不可能..我真的不明白爲什麼會發生這種情況。
有什麼想法?
編輯: // LOG4J
Hibernate:
insert
into
bytecodete.user
(email, password, type)
values
(?, ?, ?)
Hibernate:
insert
into
bytecodete.person
(birthDate, cpf, gender, idFacebook, name, tokenFacebook, idUser)
values
(?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
bytecodete.account_confirmation
(email)
values
(?)
18:45:40,745 WARN JDBCExceptionReporter:77 - SQL Error: 1452, SQLState: 23000
18:45:40,746 ERROR JDBCExceptionReporter:78 - Cannot add or update a child row: a foreign key constraint fails (`bytecodete`.`account_confirmation`, CONSTRAINT `FK_account_confirmation_1` FOREIGN KEY (`email`) REFERENCES `user` (`email`) ON DELETE CASCADE ON UPDATE CASCADE)
最好的問候, 瓦爾特·恩裏克。
難道你不應該不得不告訴hibernate用戶是外鍵嗎?也許在這種情況下是一對一的關係?我不積極如何使用註釋做到這一點。抱歉。 – hooknc 2011-04-08 20:55:15
@hooknc,它不工作,我嘗試你的建議,但不斷返回: 無法添加或更新子行:外鍵約束失敗('bytecodete'.'account_confirmation',CONSTRAINT'FK_account_confirmation_1' FOREIGN KEY('email ')參考'user'('email')ON DELETE CASCADE ON UPDATE CASCADE) – 2011-04-08 21:09:19
您應該可以打開hibernate日誌記錄來查看SQL執行的確切內容。對於log4j,您可以在DEBUG上使用「org.hibernate.SQL」。您也可以考慮在DEBUG上使用「org.hibernate」。如果你的用戶對象的映射有問題,你有該用戶對象的映射,並且你能夠添加一個用戶到你的數據庫? – hooknc 2011-04-08 21:33:31