-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: problem persisting entities with composite keys
PostPosted: Thu May 06, 2010 7:34 am 
Newbie

Joined: Thu May 06, 2010 7:02 am
Posts: 1
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:



Code:
@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;
   }



Code:
@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;
   }

}


Code:
@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;
   }




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:

Code:
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.