[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-1268) Unidirection OneToMany causes duplicate key entry violation when removing from list

Stephan van Hugten (JIRA) noreply at atlassian.com
Thu Sep 29 04:27:32 EDT 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-1268?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=43835#comment-43835 ] 

Stephan van Hugten commented on HHH-1268:
-----------------------------------------

I ran into this some days ago I think. Ultimately, it was a mapping mistake on my account, but the error it caused was similar to this.
My mapping in FOO:
@OneToMany
@JoinTable(name = "SCHEMA_FOO2BAR", 
    		joinColumns = { @JoinColumn(name = "FOO_PO_ID") }, 
    		inverseJoinColumns = { @JoinColumn(name = "BAR_PO_ID")})
private List<BAR> bars;

What I saw was that on update of this collection Hibernate first puts an EntityUpdateAction in the ActionQueue. This Entity already has the latest state of the Bag in it and will update the join table. After that it concludes that the collection is dirty and also puts one or more CollectionUpdateActions in the ActionQueue which will also update the join table ---> Exception. Seems a bit redundant. I saw this happening in the AbstractFlushingEventListener at line 282 (version 3.3.1).

> Unidirection OneToMany causes duplicate key entry violation when removing from list
> -----------------------------------------------------------------------------------
>
>                 Key: HHH-1268
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1268
>             Project: Hibernate Core
>          Issue Type: Bug
>    Affects Versions: 3.1, 3.5.6, 3.6.0
>         Environment: 3.1 final
> MySql 4.1.14 using MYISAM tables
>            Reporter: Rex Madden
>            Assignee: Gail Badner
>             Fix For: 3.2.x, 3.3.x
>
>         Attachments: possible_solution.patch, src.zip
>
>
> Simple OneToMany parent/child relationship using the default table structure (2 tables and a join table)
> Add 3 children to the parent.  Flush.  Remove the first child.  Flush throws error:
> Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
> 	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
> 	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
> 	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
> 	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
> 	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
> 	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
> 	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
> 	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
> 	at UnidirectionalOneToManyRemoveFromListBug.main(UnidirectionalOneToManyRemoveFromListBug.java:27)
> 	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:585)
> 	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86)
> Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation,  message from server: "Duplicate entry '5' for key 2"
> 	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1461)
> 	at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
> 	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
> 	... 11 more
> The problem is that there is a unique key on the relationship table that gets violated.  The session removes the last row in the relationship table, then attempts to rewrite the child_id's.  It fails since there is a uniqueness constraint on that column.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list