-->
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: Error for composite key - @IdClass
PostPosted: Sat Feb 25, 2012 11:24 pm 
Newbie

Joined: Sat Feb 25, 2012 11:12 pm
Posts: 1
I have the following error,

Hibernate generate the following

Hibernate: insert into public.divisiones (cliente_id, nombre, clienteId, divisionId) values (?, ?, ?, ?)

java.sql.BatchUpdateException: Batch entry 0 public.divisiones (cliente_id, nombre, clienteId, divisionId) values ('1', 'a', '0', '0')


But it must be

insert into public.divisiones (cliente_id, nombre, divisionId) values (?, ?, ?)

see me class.
Code:
public class DivisionPK implements Serializable {
   int clienteId;
   int divisionId;
   
   public DivisionPK(int clienteId, int divisionId) {
        this.clienteId   = clienteId;
        this.divisionId    = divisionId;
       
    }

   public DivisionPK() {
   }
   
   public int getClienteId() {
      return clienteId;
   }
   public void setClienteId(int clienteId) {
      this.clienteId = clienteId;
   }

   public int getDivisionId() {
      return divisionId;
   }

   public void setDivisionId(int divisionId) {
      this.divisionId = divisionId;
   }

    public boolean equals(Object object) {
           if (object instanceof DivisionPK) {
              DivisionPK pk = (DivisionPK)object;
               return clienteId == pk.clienteId && divisionId == pk.divisionId;
           } else {
               return false;
           }
       }
   
       public int hashCode() {
           return clienteId + divisionId ;
       }

}

@Entity
@IdClass(DivisionPK.class)
@Table(name="divisiones", schema = "public")

public class Division implements Serializable {
   
   
   public Division()
   {}
   
   public Division(Cliente cliente)
   {
      this.cliente = cliente;
      this.clienteId = cliente.getId();
   }


   @Id
   private int divisionId;
   
   @GeneratedValue
   @Column(name="division_id")
   public int getDivisionId() {
      return divisionId;
   }

   public void setDivisionId(int divisionId) {
      this.divisionId = divisionId;
   }
   

   private String nombre;

   @Column(name="division_nombre")
   public String getNombre() {
      return nombre;
   }

   public void setNombre(String nombre) {
      this.nombre = nombre;
   }
   
   @Id
   @Column(name="cliente_id", insertable=false, updatable=false)
   private int clienteId;
   
   @ManyToOne
    @JoinColumn(name="cliente_id")
   private Cliente cliente;
   
   
   public Cliente getCliente() {
      return cliente;
   }

   public void setCliente(Cliente cliente) {
      this.cliente = cliente;
   }
   
      
}


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.