Hibernate mapping one-to-many problem

Posted by Xorty on Stack Overflow See other posts from Stack Overflow or by Xorty
Published on 2011-01-16T01:11:08Z Indexed on 2011/01/16 1:53 UTC
Read the original article Hit count: 590

Filed under:
|
|

Hello, I am not very experienced with Hibernate and I am trying to create one-to-many mapping.

Here are relevant tables: alt text

And here are my mapping files:

<hibernate-mapping package="com.xorty.mailclient.server.domain">
  <class name="Attachment" table="Attachment">
    <id name="id">
        <column name="idAttachment"></column>
    </id>
    <property name="filename">
        <column name="name"></column>
    </property>
    <property name="blob">
        <column name="file"></column>
        <type name="blob"></type>
    </property>
    <property name="mailId">
        <column name="mail_idmail"></column>
    </property>
  </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="com.xorty.mailclient.server.domain.Mail" table="mail">
        <id name="id" type="integer" column="idmail"></id>
        <property name="content">
            <column name="body"></column>
        </property>
        <property name="ownerAddress">
            <column name="account_address"></column>
        </property>
        <property name="title">
            <column name="head"></column>
        </property>
        <set name="receivers" table="mail_has_contact" cascade="all">
            <key column="mail_idmail"></key>
            <many-to-many column="contact_address" class="com.xorty.mailclient.client.domain.Contact"></many-to-many>
        </set>
        <list name="attachments" cascade="save-update, delete" inverse="true">
            <key column="mail_idmail" not-null="true"/>
            <index column="fk_Attachment_mail1"></index>
            <one-to-many class="com.xorty.mailclient.server.domain.Attachment"/>
        </list>
    </class>
</hibernate-mapping>

In plain english, one mail has more attachments. When I try to do CRUD on mail without attachments, everyting works just fine. When I add some attachment to mail, I cannot perform any CRUD operation.

I end up with following trace:

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at domain.DatabaseTest.testPersistMailWithAttachment(DatabaseTest.java:355)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    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 (`maildb`.`attachment`, CONSTRAINT `fk_Attachment_mail1` FOREIGN KEY (`mail_idmail`) REFERENCES `mail` (`idmail`) ON DELETE NO ACTION ON UPDATE NO ACTION)
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 27 more

Thank you

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate