1 Reply Latest reply on May 6, 2010 2:50 PM by javiero09

    problem: many-to-many association with composite key

    javiero09

      The problem is when we have the next case: we have a many to many relation between identificacion and entidad. The relation table between both have attributes, then is mapped like this   (ALL GENERATED BY SEAM-GEN): 





      @Entity
      @Table(name = "identificacion", schema = "etr")
      public class Identificacion implements java.io.Serializable {

              private int codigoEtr;
              ....... more properties .......

              private Set<IdentificacionEntidades> identificacionEntidadeses = new HashSet<IdentificacionEntidades>(0);
             

              public Identificacion() {
              }


              @Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
              @Column(name = "codigo_etr", unique = true, nullable = false)
              public int getCodigoEtr() {
                      return this.codigoEtr;
              }

              public void setCodigoEtr(int codigoEtr) {
                      this.codigoEtr = codigoEtr;
              }


              @OneToMany(fetch = FetchType.LAZY, mappedBy = "identificacion",cascade=CascadeType.ALL)
              public Set<IdentificacionEntidades> getIdentificacionEntidadeses() {
                      return this.identificacionEntidadeses;
              }

              public void setIdentificacionEntidadeses(
                              Set<IdentificacionEntidades> identificacionEntidadeses) {
                      this.identificacionEntidadeses = identificacionEntidadeses;
              }





      @Entity
      @Table(name = "identificacion_entidades", schema = "etr")
      public class IdentificacionEntidades implements java.io.Serializable {    // this is the relation table between identificacion and entidad

              private IdentificacionEntidadesId id;   // composite key that contains two identifiers of relationated  classes
              private Entidad entidad;
              private Identificacion identificacion;
              private String codigo;

              public IdentificacionEntidades() {
              }

              public IdentificacionEntidades(IdentificacionEntidadesId id,
                              Entidad entidad, Identificacion identificacion, String codigo) {
                      this.id = id;
                      this.entidad = entidad;
                      this.identificacion = identificacion;
                      this.codigo = codigo;
              }


             
              @EmbeddedId
              @AttributeOverrides( {
                              @AttributeOverride(name = "codigoEtr", column = @Column(name = "codigo_etr",insertable=true,updatable=true, nullable = false)),
                              @AttributeOverride(name = "idEntidad", column = @Column(name = "id_entidad",insertable=true,updatable=true, nullable = false)) })
              @NotNull
              public IdentificacionEntidadesId getId() {
                      return this.id;
              }

              public void setId(IdentificacionEntidadesId id) {
                      this.id = id;
              }

              @ManyToOne(fetch = FetchType.LAZY)
              @JoinColumn(name = "id_entidad", nullable = false, insertable = false, updatable = false)
              @NotNull
             
              public Entidad getEntidad() {
                      return this.entidad;
              }

              public void setEntidad(Entidad entidad) {
                      this.entidad = entidad;
              }

              @ManyToOne(fetch = FetchType.LAZY)
              @JoinColumn(name = "codigo_etr", nullable = false, insertable = false, updatable = false)
              @NotNull
              public Identificacion getIdentificacion() {
                      return this.identificacion;
              }

              public void setIdentificacion(Identificacion identificacion) {
                      this.identificacion = identificacion;
              }

              @Column(name = "codigo", nullable = false, length = 200)
              @NotNull
              @Length(max = 200)
              public String getCodigo() {
                      return this.codigo;
              }

              public void setCodigo(String codigo) {
                      this.codigo = codigo;
              }

      }




      @Embeddable
      public class IdentificacionEntidadesId implements java.io.Serializable {

              private int codigoEtr;
              private int idEntidad;

              public IdentificacionEntidadesId() {
              }

              public IdentificacionEntidadesId(int codigoEtr, int idEntidad) {
                      this.codigoEtr = codigoEtr;
                      this.idEntidad = idEntidad;
              }

              @Column(name = "codigo_etr")
              public int getCodigoEtr() {
                      return this.codigoEtr;
              }

              public void setCodigoEtr(int codigoEtr) {
                      this.codigoEtr = codigoEtr;
              }

              @Column(name = "id_entidad")
              public int getIdEntidad() {
                      return this.idEntidad;
              }

              public void setIdEntidad(int idEntidad) {
                      this.idEntidad = idEntidad;
              }
      [/code]



      the problem is when persist a "identificacion", the cascade can't establish id's of the relation table:

      [code]
      identificacionEntidadesHome.getInstance().setEntidad(entidad);
      identificacionEntidadesHome.getInstance().setIdentificacion(identificacionHome.getInstance());
      identificacionHome.getInstance().getIdentificacionEntidadeses().add(identificacionEntidadesHome.getInstance());
      identificacionHome.persist();



      The error is the next:



      insert
          into
              etr.identificacion
              (id_autoridad, id_bien, caracteristicas_tecnicas, id_clasificacion_generica, id_comarca, componentes, cuadro, id_elm, espacio, id_municipio, pre_tipo, id_provincia, sector, sintesis_historica, id_tipo_objeto, ubicacion_inmueble, ue, id_yacimiento, codigo_etr)
          values
              (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)      // Insert the first element good
      13:30:40,651 INFO  [STDOUT] Hibernate:
          insert
          into
              etr.identificacion_entidades
              (codigo, codigo_etr, id_entidad)
          values
              (?, ?, ?)                             // but when have to insert second, don't have the keys
      13:30:40,730 WARN  [JDBCExceptionReporter] SQL Error: 0, SQLState: 23503
      13:30:40,730 ERROR [JDBCExceptionReporter] Batch entry 0 insert into etr.identificacion_entidades (codigo, codigo_etr, id_entidad) values ('somethink', '0', '0') was aborted.  Call getNextException to see the cause.
      13:30:40,730 WARN  [JDBCExceptionReporter] SQL Error: 0, SQLState: 23503
      13:30:40,730 ERROR [JDBCExceptionReporter] ERROR: inserción o actualización en la tabla «identificacion_entidades» viola la llave foránea «fk_etr_identificacion_entidades_id_entidad»
        Detail: La llave (id_entidad)=(0) no está presente en la tabla «entidad».




      it seem not work with composed keys, may be a bug???
      thank for your atention.

        • 1. Re: problem: many-to-many association with composite key
          javiero09

          sorry bad format of the text:



          The problem is when we have the next case: we have a many to many relation between identificacion and entidad. The relation table between both have attributes, then is mapped like this: 






          @Entity
          @Table(name = "identificacion", schema = "etr")
          public class Identificacion implements java.io.Serializable {
          
               private int codigoEtr;
               ....... more properties .......
          
               private Set<IdentificacionEntidades> identificacionEntidadeses = new HashSet<IdentificacionEntidades>(0);
               
          
               public Identificacion() {
               }
          
          
               @Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
               @Column(name = "codigo_etr", unique = true, nullable = false)
               public int getCodigoEtr() {
                    return this.codigoEtr;
               }
          
               public void setCodigoEtr(int codigoEtr) {
                    this.codigoEtr = codigoEtr;
               }
          
          
               @OneToMany(fetch = FetchType.LAZY, mappedBy = "identificacion",cascade=CascadeType.ALL)
               public Set<IdentificacionEntidades> getIdentificacionEntidadeses() {
                    return this.identificacionEntidadeses;
               }
          
               public void setIdentificacionEntidadeses(
                         Set<IdentificacionEntidades> identificacionEntidadeses) {
                    this.identificacionEntidadeses = identificacionEntidadeses;
              }







          @Entity
          @Table(name = "identificacion_entidades", schema = "etr")
          public class IdentificacionEntidades implements java.io.Serializable {    // this is the relation table between identificacion and entidad
          
               private IdentificacionEntidadesId id;   // composite key that contains two identifiers of relationated  classes
                private Entidad entidad;
               private Identificacion identificacion;
               private String codigo; 
          
               public IdentificacionEntidades() {
               }
          
               public IdentificacionEntidades(IdentificacionEntidadesId id,
                         Entidad entidad, Identificacion identificacion, String codigo) {
                    this.id = id;
                    this.entidad = entidad;
                    this.identificacion = identificacion;
                    this.codigo = codigo;
               }
          
          
               
               @EmbeddedId
               @AttributeOverrides( {
               @AttributeOverride(name = "codigoEtr", column = @Column(name = "codigo_etr",insertable=true,updatable=true, nullable = false)),
               @AttributeOverride(name = "idEntidad", column = @Column(name = "id_entidad",insertable=true,updatable=true, nullable = false)) })
               @NotNull
               public IdentificacionEntidadesId getId() {
                    return this.id;
               }
          
               public void setId(IdentificacionEntidadesId id) {
                    this.id = id;
               }
          
               @ManyToOne(fetch = FetchType.LAZY)
               @JoinColumn(name = "id_entidad", nullable = false, insertable = false, updatable = false)
               @NotNull
               
               public Entidad getEntidad() {
                    return this.entidad;
               }
          
               public void setEntidad(Entidad entidad) {
                    this.entidad = entidad;
               }
          
               @ManyToOne(fetch = FetchType.LAZY)
               @JoinColumn(name = "codigo_etr", nullable = false, insertable = false, updatable = false)
               @NotNull
               public Identificacion getIdentificacion() {
                    return this.identificacion;
               }
          
               public void setIdentificacion(Identificacion identificacion) {
                    this.identificacion = identificacion;
               }
          
               @Column(name = "codigo", nullable = false, length = 200)
               @NotNull
               @Length(max = 200)
               public String getCodigo() {
                    return this.codigo;
               }
          
               public void setCodigo(String codigo) {
                    this.codigo = codigo;
               }
          
          }









          @Embeddable
          public class IdentificacionEntidadesId implements java.io.Serializable {
          
               private int codigoEtr;
               private int idEntidad;
          
               public IdentificacionEntidadesId() {
               }
          
               public IdentificacionEntidadesId(int codigoEtr, int idEntidad) {
                    this.codigoEtr = codigoEtr;
                    this.idEntidad = idEntidad;
               }
          
               @Column(name = "codigo_etr")
               public int getCodigoEtr() {
                    return this.codigoEtr;
               }
          
               public void setCodigoEtr(int codigoEtr) {
                    this.codigoEtr = codigoEtr;
               }
          
               @Column(name = "id_entidad")
               public int getIdEntidad() {
                    return this.idEntidad;
               }
          
               public void setIdEntidad(int idEntidad) {
                    this.idEntidad = idEntidad;
               }
          [/code]
          
          
          
          the problem is when persist a "identificacion", the cascade can't establish id's of the relation table:
          
          [code]
          identificacionEntidadesHome.getInstance().setEntidad(entidad);
          identificacionEntidadesHome.getInstance().setIdentificacion(identificacionHome.getInstance()); 
          identificacionHome.getInstance().getIdentificacionEntidadeses().add(identificacionEntidadesHome.getInstance()); 
          identificacionHome.persist();







          The error is the next:





          insert 
              into
                  etr.identificacion
                  (id_autoridad, id_bien, caracteristicas_tecnicas, id_clasificacion_generica, id_comarca, componentes, cuadro, id_elm, espacio, id_municipio, pre_tipo, id_provincia, sector, sintesis_historica, id_tipo_objeto, ubicacion_inmueble, ue, id_yacimiento, codigo_etr) 
              values
                  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)      // Insert the first element good
          13:30:40,651 INFO  [STDOUT] Hibernate: 
              insert 
              into
                  etr.identificacion_entidades
                  (codigo, codigo_etr, id_entidad) 
              values
                  (?, ?, ?)                             // but when have to insert second, don't have the keys
          13:30:40,730 WARN  [JDBCExceptionReporter] SQL Error: 0, SQLState: 23503
          13:30:40,730 ERROR [JDBCExceptionReporter] Batch entry 0 insert into etr.identificacion_entidades (codigo, codigo_etr, id_entidad) values ('somethink', '0', '0') was aborted.  Call getNextException to see the cause.
          13:30:40,730 WARN  [JDBCExceptionReporter] SQL Error: 0, SQLState: 23503
          13:30:40,730 ERROR [JDBCExceptionReporter] ERROR: inserción o actualización en la tabla «identificacion_entidades» viola la llave foránea «fk_etr_identificacion_entidades_id_entidad»
            Detail: La llave (id_entidad)=(0) no está presente en la tabla «entidad».






          it seem not work with composed keys, may be a bug???
          thank for your atention.