5 Replies Latest reply on Dec 7, 2006 10:51 AM by martinwhs

    ManyToMany constraints problem

    melampo

      Hello.

      I have some problems using a ManyToMany relationship model. I think that it is an error in my implementation (probably with the CascadeType), but I don't know how to mend it.

      I have this model:

      public class Libro implements Serializable {
      
       ...
       @ManyToMany (cascade = {CascadeType.PERSIST})
       @JoinTable(name = "LibrosEtiquetas", joinColumns = { @JoinColumn(name = "idLibro") }, inverseJoinColumns = { @JoinColumn(name = "idEtiqueta") })
       private List<Etiqueta> etiquetas;
       ...
      
      


      public class Etiqueta implements Serializable {
      
       ....
       @ManyToMany(cascade = { CascadeType.PERSIST }, mappedBy = "etiquetas")
       private List<Libro> libros;
       ....
      


      When I persist a "libro" and the List is filled with "etiquetas" that doesn't exist in the database, it woks ok. It create the new "libro", the new "etiquetas" and the relation in the join table.

      But if I persist with any existent "etiqueta" in the List it returns me an exception because it tries to create the "etiqueta" again... I don't know how can get that it does not try to create an "etiqueta" which is already created.

      Caused by: java.sql.BatchUpdateException: Duplicate entry 'dos' for key 1
       at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:657)
       at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:517)
       at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
      



      Thank, and sorry for my english :D

        • 1. Re: ManyToMany constraints problem

          I am having the same problem. Did you ever solve this issue?

          - Kevin

          • 2. Re: ManyToMany constraints problem
            melampo

            No, sorry... I have not solve it...

            • 3. Re: ManyToMany constraints problem

              I've solved my problem which i belive is the same as yours. My solution was to use the em.merge instead of a em.persist. This is weird because my Owner object is new e.g. (has never been persist before). But this works!!!!! So basicly i have a check to see if child object has a known ID if so then merge else call persist.

              - Kevin

              • 4. Re: ManyToMany constraints problem
                melampo

                Ok... I am going to try that.

                Thanks.

                • 5. Re: ManyToMany constraints problem
                  martinwhs

                  Is there a reason for the usage of CascadeType.PERSIST on the many-to-many relationship in the etiquetas class?

                  Since that very relationship is mapped by the etiquetas field in the libros class, you probably won't ever manipulate the collection of libros in the etiquetas, but only do reads on it. Therefor I would suggest removing the cascadeType in the etiquetas class.

                  The decision wether to use cascading on relationships affects the way you use the entitymanager. You can do everything without cascading, it is just less comfortable (more persist and merge calls). In this very case you would have to manage the etiquetas seperatly. This is maybe a good idea, since they don't seem to form a composition with libros (can live without them).

                  If you don't feel comfortable with cascading, then you can turn it off and do things manually.

                  Best regards,
                  Martin