4 Replies Latest reply on Feb 3, 2011 4:58 AM by Eduardo Fernández Alonso

    Generate an auto id

    Eduardo Fernández Alonso Newbie

      Well, my problem seams to be easy to solve but i can´t fix it. I´m a newbie on Hibernate programming. I´m in a Seam&Hibernate project and I need to generate an automatic value for an id on my table. I´ve read all Hibernate documentation and nothing of that works for me.

       

      I´m using Hibernate 3.6,Jboss 5.1.0 GA and Postgres9.0 and I´m doing this:

       

      My table script:

       

      CREATE TABLE aisjcrm.zona

      (

      empresa character varying(4) NOT NULL DEFAULT ''::character varying,

      zona character varying(40) NOT NULL DEFAULT ''::character varying,

      descripcion character varying(80) NOT NULL DEFAULT ''::character varying,

      permitemodificar boolean NOT NULL DEFAULT false,

      usr_creacion character varying(40) DEFAULT ''::character varying,

      fch_creacion timestamp without time zone,

      usr_modif character varying(40) DEFAULT ''::character varying,

      fch_modif timestamp without time zone,

      id_zona serial NOT NULL, <- That´s the value to generate

      CONSTRAINT pk_zona PRIMARY KEY (empresa, zona, id_zona),

      CONSTRAINT id_zona UNIQUE (id_zona)

      )

       

      My model code:

       

      @GeneratedValue

      private int idZona;

      ...

      ...

      ...

      @Column(name = "id_zona", nullable = false)

      @NotNull

      public Integer getIdZona() {

      return this.idZona;

      }

       

      I also have try this:

       

      @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_GEN")

      @javax.persistence.SequenceGenerator(name="SEQ_GEN",sequenceName="my_sequence")

       

      Using any of this examples I always get the same persistence exception: "Batch entry 0 insert into aisjcrm.zona (descripcion, fch_creacion, fch_modif, permitemodificar, usr_creacion, usr_modif, empresa, id_zona, zona) values ('cx', NULL, NULL, '0', NULL, NULL, 'cx', '0', 'cx')" was aborted. Call getNextException to see the cause. This is because hibernate doesn´t generate a value for id_zona and always try to insert a 0 for this field. I´ve check that there isn´t a point on my code where I give a value to id_zona. So I think that the 0 value is generate by Hibernate cause id_zona is an integer or it doesn´t?

       

      ¿What do i´m doing wrong? ¿Do i have to write something on any other file to configure it?

      ¿Is a Hibernate problem or a Seam problem?

       

      Thank you in advance! And sorry for my english.