Cannot add or update a child row: a foreign key constraint fails




Asked on May 21, 2013
Hi All, I am running @ManyToMany association, but got below error. Help me to resolve it.

Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`testdb`.`a_b`, CONSTRAINT `FK2E2EF2DEA3DDB7DE` FOREIGN KEY (`b_id`) REFERENCES `b` (`b_id`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 8 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`testdb`.`a_b`, CONSTRAINT `FK2E2EF2DEA3DDB7DE` FOREIGN KEY (`b_id`) REFERENCES `b` (`b_id`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415)
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1976)




Replied on May 21, 2013
Post your code snippet where you are using @ManyToMany



Replied on May 21, 2013
Tom, find the code snippet

    @ManyToMany(
)
    @JoinTable(
        name="a_b",
        joinColumns=@JoinColumn(name="a_id"),
        inverseJoinColumns=@JoinColumn(name="b_id")
    )
private Set<B> setb;



Replied on May 21, 2013
You are missing to use targetEntity and cascade=CascadeType, you can use it as
 @ManyToMany(
            targetEntity=B.class,
            cascade=CascadeType.ALL
    )




Write Answer











©2024 concretepage.com | Privacy Policy | Contact Us