سؤال

I'm having trouble setting up a bi-directional relationship in Hibernate (v 3.2.1.g a -- can't upgrade now). I have an object, TrainingSession, which contains many TrainingLink objects. So I have

@Entity
@Table(name = "cb_pd_training_session",
    uniqueConstraints = {@UniqueConstraint(columnNames={"ORDER_NUMBER"})}
)
public class TrainingSession {
...    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "trainingSession")
    private Set<TrainingLink> links;
    …
    public void addTrainingLink(final TrainingLink link)
    {
        if (this.links == null)
        {
            this.links = new HashSet<TrainingLink>();
        }
        link.setTrainingSessionId(this);
        this.links.add(link);
    }

and the child entity …

@Entity
@Table(name = "cb_training_link",
    uniqueConstraints = {@UniqueConstraint(columnNames={"LINK"})}
)
public class TrainingLink {
    …
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "TRAINING_SESSION_ID")
    @NotNull
    private TrainingSession trainingSession;

but when I try and create a parent object, add a child entity and save, I'm getting an error. What else do I need to do to set this up properly?

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java   298)
    at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:41)
    at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1562)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
    at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:305)
    at org.mainco.subco.client.dao.myproject.AbstractHibernateDAO.findByUniqueConstraint(AbstractHibernateDAO.java:63)
    at org.mainco.subco.client.dao.myproject.TrainingSessionDAOImpl.findByOrderNumber(TrainingSessionDAOImpl.java:18)
    at org.mainco.subco.dao.TrainingSessionDAOTest.testSaveTrainingSessionTrainerAndLink(TrainingSessionDAOTest.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`myproject`.`cb_training_link`, CONSTRAINT `FK1_CB_TRAINING_LINK` FOREIGN KEY (`TRAINING_SESSION_ID`) REFERENCES `CB_TRAINING_SESSION` (`ID`) ON DELETE CASCADE)
    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:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
    ... 36 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`myproject`.`cb_training_link`, CONSTRAINT `FK1_CB_TRAINING_LINK` FOREIGN KEY (`TRAINING_SESSION_ID`) REFERENCES `CB_TRAINING_SESSION` (`ID`) ON DELETE CASCADE)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    at com.mysql.jdbc.Util.getInstance(Util.java:382)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
    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)
    ... 39 more
هل كانت مفيدة؟

المحلول

Turns out the problem was when I set up my underlying MySQL table (using v 5.5), I created a foreign key to a table that didn't exist. Strange how MySQL didn't warn me about that when I initially created the table, but oh well.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top