我有以下实体:
@Entity
@Table(name = "chat",
uniqueConstraints = {
@UniqueConstraint(columnNames = {"user_1", "user_2"})
})
public class Chat {
@ManyToOne
@JoinColumn(name = "user_1")
@OnDelete(action = OnDeleteAction.CASCADE)
private User user1;
@ManyToOne
@JoinColumn(name = "user_2")
@OnDelete(action = OnDeleteAction.CASCADE)
private User user2;
}
用户类:
@Entity
@Table(name="users",
uniqueConstraints={
@UniqueConstraint(columnNames={"company_id", "username"})
}
)
public class User {
@Id
@GenericGenerator(name = "uuid-gen", strategy = "uuid2")
@GeneratedValue(generator = "uuid-gen",strategy=GenerationType.IDENTITY)
private String id;
// there is no field/reference to Chat entity
}
用户实体没有任何对Chat实体的引用。我需要删除用户的聊天记录。问题是用户ID(我想删除)可以在user1
或user2
字段中。例如,我有用户A和用户B.他们有聊天C.如果我尝试删除,例如,用户A,它应该删除用户A并聊聊C.但是如果提供配置,我有以下错误:
Cannot delete or update a parent row: a foreign key constraint fails
(`mydb`.`chat`, CONSTRAINT `FKqslncg7pcc89gvjjpp9jypbha`
FOREIGN KEY (`user_2`) REFERENCES `users` (`id`))
作为可能的解决方案,我使用了this answer。但是使用
entityManager.remove(user);
entityManager.clear();
无济于事。另外,我检查了ddl代码,没有提到Cascade动作。 如何解决这个问题?
答案 0 :(得分:0)
我找到了解决方案。您需要的第一件事是添加人类可读的ForeignKey约束并删除@ManyToOne
@JoinColumn(name = "user_1", foreignKey = @ForeignKey(name = "user_1_fk"))
private User user1;
@ManyToOne
@JoinColumn(name = "user_2", foreignKey = @ForeignKey(name = "user_2_fk"))
private User user2;
。现在我的代码看起来像这样:
ALTER TABLE chat DROP FOREIGN KEY `user_1_fk`;
ALTER TABLE chat DROP FOREIGN KEY `user_2_fk`;
ALTER TABLE chat
ADD CONSTRAINT `user_1_fk`
FOREIGN KEY (`user_1` )
REFERENCES `users` (`id` )
ON DELETE CASCADE;
ALTER TABLE chat
ADD CONSTRAINT `user_2_fk`
FOREIGN KEY (`user_2` )
REFERENCES `users` (`id` )
ON DELETE CASCADE;
然后我删除了这个表,启动了一个应用程序,允许hibernate使用注释中提供的正确FK名称重新创建此表。然后我使用以下SQL打开了MySQL工作台并修改了该表的外键:
React-Router
这就是全部。