2 Replies Latest reply on Mar 20, 2009 5:17 PM by sridhar kondoji

    Please clear my confusion on @GeneratedValue annotation

    sridhar kondoji Newbie
      I wanted to to use default AUTO strategy which i expected Hibernate to generate unique ID's to be able to insert intoPrimary Key column.
      However this didnot happen and i got insert exception "Field Id doesnot have a default value".

      These are the steps i followed.
      1) Created my tables in MySql first (manually).
         CREATE TABLE userprofile
      (
           id BIGINT NOT NULL,     
           name TINYTEXT NOT NULL,     
           gender ENUM ('male','female') NOT NULL,
           createdon   DATETIME NOT NULL,
           lastupdated DATETIME NULL,
           PRIMARY KEY (id)
      );
      I am not using any Auto Increment in MySql because i expected Hibernate to generate/insert unique values in column id.
      2) I then used seam tool to setup project and generated entities.
      This is how my entity looks like
      @Entity
      @Table(name = "userprofile")
      public class Userprofile implements java.io.Serializable
      {

           private long id;
           
           @Id
           @GeneratedValue(strategy = IDENTITY)
           @Column(name = "id", unique = true, nullable = false)
           public long getId()
           {
                return this.id;
           }


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

      ....
      }

      3) Now after deploying, iam trying to create userprofile, ut receiving sql exception
      "Fiedl id does not have default value"

      4) My Persistent unit is defined as follows
            <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
               <property name="hibernate.hbm2ddl.auto" value="update"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/>
               <property name="jboss.entity.manager.factory.jndi.name" value="java:/partiesEntityManagerFactory"/>
               <property name="hibernate.default_catalog" value="sample"/>
            </properties>

      Why am i not able to insert anything in mysql table?
      The error message indicates as if Hibernate is not generating unique values but is depending on mysql to create for it.
      If that is the case, what is the purpose of @GeneratedValue annotation?
      I tried all strategies applicable but still received the same error.

      Thanks in advance.
      --sri