简体   繁体   English

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

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

SOLVED 解决了

Thanks to Predrag Maric I managed to find a bug which was that referencedColumnName was not referring to the right field. 感谢Predrag Maric,我设法找到了一个bug,其中referencedColumnName没有引用正确的字段。 then I had to make the Table2 class implement the Serializable Interface and this made my test pass 然后我不得不让Table2类实现Serializable接口,这使我的测试通过

Thanks for your help 谢谢你的帮助

Question

How can I map Table3 in Hibernate? 如何在Hibernate中映射Table3?

在此输入图像描述

This is my attempt: 这是我的尝试:

@Entity
@Table(name="Table3")
public class Table3 implements Serializable
{
    @Id
    @GeneratedValue
    private long id;

    @NotNull
    @Column(name="field1")
    private String field1;

    @NotNull
    @Column(name="field2")
    private String field2;

    @ManyToOne
    @JoinColumn(name="Table2_id")
    private Table2 Table2_id ;

    @ManyToOne
    @JoinColumn(name="Table2_Table1_username")
    private Table2 Table2_Table1_username;

The error is in my JUnit test: 错误发生在我的JUnit测试中:

Cannot add or update a child row: a foreign key constraint fails ( database . Table3 , CONSTRAINT fk_Table3_Table2 FOREIGN KEY ( Table2_id , Table2_Table1_username ) REFERENCES Table2 ( id , Table1_username ) ON DELETE NO ACTION ON UPDATE NO ACTION) Cannot add or update a child row: a foreign key constraint fails (数据库.表3 , CONSTRAINT fk_Table3_Table2 FOREIGN KEY ( Table2_id , Table2_Table1_username ) REFERENCES Table2 ( id , Table1_username ) ON DELETE NO ACTION ON UPDATE NO ACTION)

Constructor: 构造函数:

Table3(String field1, String field2, Table2 table)
{
    this.field1 = field1;
    this.field2 = field2;
    this.Table2_id = table;
    this.Table2_Table1_username = table;
} 

JUnit Test JUnit测试

@Before
    public void init() 
    {
        JdbcTemplate jdbc = new JdbcTemplate(dataSource);
        jdbc.execute("DELETE FROM Table3");
        jdbc.execute("DELETE FROM Table2");
        jdbc.execute("DELETE FROM Table1");
    }
 @Test
    public void testCreateMedia()
    {
        table1DAO.createTable(table1Object);
        table2DAO.createTable(table2Object);
        table3DAO.createTable(table3Object); //fails here
    }

Noted: I am not using java naming convention for variables 注意:我没有使用变量的java命名约定

I think you misunderstood name and referencedColumnName in @JoinColumn . 我认为你在@JoinColumn误解了namereferencedColumnName

name is the name of the column in source ( Table3 ) table which is a foreign key to target ( Table2 ) table. name是source( Table3 )表中列的名称,该表是目标( Table2 )表的外键。

referencedColumnName is the name of the column in target table which is a primari key ( Table2.id ). referencedColumnName是目标表中列的名称,它是主键( Table2.id )。 This attribute can be omitted in most cases. 在大多数情况下,可以省略此属性。

In your case, it should look like this 在你的情况下,它应该是这样的

@ManyToOne
@JoinColumn(name="Table2_id")
private Table2 Table2_id ;

@ManyToOne
@JoinColumn(name="Table2_Table1_username")
private Table2 Table2_Table1_username;

暂无
暂无

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

相关问题 无法添加或更新子行:外键约束在休眠状态下失败 - Cannot add or update a child row: a foreign key constraint fails in hibernate 休眠:无法添加或更新子行:外键约束失败 - Hibernate:Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - Hibernate - Cannot add or update a child row: a foreign key constraint fails - Hibernate Hibernate:无法添加或更新子行:外键约束失败 - Hibernate :Cannot add or update a child row: a foreign key constraint fails Hibernate 无法添加或更新子行:外键约束失败 - Hibernate Cannot add or update a child row: a foreign key constraint fails 尝试在Hibernate中添加主复合键时,“无法添加或更新子行:外键约束失败” - 'Cannot add or update a child row: a foreign key constraint fails' when trying to add a primary composite key in Hibernate Hibernate @OneToOne 无法添加或更新子行:外键约束 - Hibernate @OneToOne cannot add or update a child row: a foreign key constraint 休眠级联保存错误(无法添加或更新子行:外键约束失败) - Hibernate cascade saving error (Cannot add or update a child row: a foreign key constraint fails) Hibernate:java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - Hibernate: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails 错误:无法添加或更新子行:使用mysql在hibernate中出现外键约束失败 - ERROR: Cannot add or update a child row: a foreign key constraint fails in hibernate using mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM