繁体   English   中英

休眠:无法添加或更新子行:外键约束失败

[英]Hibernate:Cannot add or update a child row: a foreign key constraint fails

当我尝试使用休眠方式插入(保存)用户时,此错误给了我:

//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)

最好的问候,Valter Henrique。

您是否在同一会话中保存用户和帐户确认? 这是双向关系还是单向关系? (也许您也应该发布班级用户的内容)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM