title: derby + hibernate ConstraintViolationException using manytomany relationships
body: <p>I'm new to Hibernate+Derby... I've seen this issue mentioned throughout the google, but have not seen a proper resolution.</p>
  
  <p>This following code works fine with mysql, but when I try this on derby i get exceptions:</p>
  
  <p>( each Tag has two sets of files and vise-versa - manytomany)</p>
  
  <p><strong>Tags.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="TAGS")
  public class Tags implements Serializable 
  {
  
     @Id @GeneratedValue(strategy=GenerationType.AUTO)
     public long getId()
     {
        return id;
     }
  
     @ManyToMany(targetEntity=Files.class
     )
     @ForeignKey(name="USER_TAGS_FILES",inverseName="USER_FILES_TAGS")
     @JoinTable(name="USERTAGS_FILES",
           joinColumns=@JoinColumn(name="TAGS_ID"),
           inverseJoinColumns=@JoinColumn(name="FILES_ID"))
           public Set&lt;data.Files&gt; getUserFiles()
           {
        return userFiles;
           }
  
     @ManyToMany(mappedBy="autoTags",
           targetEntity=data.Files.class)
           public Set&lt;data.Files&gt; getAutoFiles()
           {
        return autoFiles;
           }
  </code></pre>
  
  <p><strong>Files.java</strong></p>
  
  <pre><code>
  @Entity
  @Table(name="FILES")
  public class Files implements Serializable
  {
     @Id @GeneratedValue(strategy=GenerationType.AUTO)
     public long getId()
     {
        return id;
     }
        @ManyToMany(mappedBy="userFiles",
           targetEntity=data.Tags.class)
     public Set getUserTags()
     {
        return userTags;
     }
     @ManyToMany(targetEntity=Tags.class
           )
           @ForeignKey(name="AUTO_FILES_TAGS",inverseName="AUTO_TAGS_FILES")
     @JoinTable(name="AUTOTAGS_FILES",
           joinColumns=@JoinColumn(name="FILES_ID"),
           inverseJoinColumns=@JoinColumn(name="TAGS_ID"))
     public Set getAutoTags()
     {
        return autoTags;
     }
  </code></pre>
  
  <p>I add some data to the DB, but when running over Derby these exception turn up (the don't using mysql)</p>
  
  <p><strong>Exceptions</strong></p>
  
  <pre><code>
  SEVERE: DELETE on table 'FILES' caused a violation of foreign key constraint 'USER_FILES_TAGS' for key (3).  The statement has been rolled back.
  Jun 10, 2010 9:49:52 AM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
  SEVERE: Could not synchronize database state with session
  org.hibernate.exception.ConstraintViolationException: could not delete: [data.Files#3]
     at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
     at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2712)
     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2895)
     at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
     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:1206)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     at java.lang.reflect.Method.invoke(Method.java:613)
     at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
     at $Proxy13.flush(Unknown Source)
     at data.HibernateORM.removeFile(HibernateORM.java:285)
     at data.DataImp.removeFile(DataImp.java:195)
     at booting.DemoBootForTestUntilTestClassesExist.main(DemoBootForTestUntilTestClassesExist.java:62)
  </code></pre>
  
  <p>I have never used derby before so maybe there is something crutal that i'm missing<br>
  1) what am I doing wrong? <br>
  2) is there any way of cascading properly when I have 2 many-to-many relationships between two classes?</p>
  
  <p>Thanks!</p>