4 Replies Latest reply on Feb 3, 2011 4:58 AM by edufer

    Generate an auto id

    edufer

      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.

        • 1. Generate an auto id
          nickarls

          You still need @Id on the primary key

          • 2. Generate an auto id
            edufer

            Hello Nicklas,

             

            I can´t insert the @id annotation because this table have a 3-part primary key with these fields:empresa, zona_id and zona. So I have insert the @Embeddable annotation. This is my ZonaId.java:

             

            @Embeddable

            public class ZonaId implements Serializable {

                      private String empresa;

                      private String zona;

              @Generated

                      private Integer idZona;

            ...

            ...

            ...

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

                      @NotNull

                      public Integer getIdZona() {

                                return idZona;

                      }

             

             

                      public void setIdZona(Integer idZona) {

                                this.idZona = idZona;

                      }

             

            Thanks you

            1 of 1 people found this helpful
            • 3. Generate an auto id
              nickarls

              Well, you need some sort of @Id or @EmbeddedId anyway. I don't think you can do @Generated on a single field of a composite key but I could be wrong, never tried it.

              1 of 1 people found this helpful
              • 4. Generate an auto id
                edufer

                Well, sincerly I´ve thought it cause i hadn´t read any example whit EmbeddedId. Any way, thanks you Nicklas.

                 

                If i find a solution I´ll tell you