3 Replies Latest reply on Mar 1, 2007 3:20 PM by Tony Mai

    How to use @Id @GeneratedValue(strategy=GenerationType.AUTO)

    Tony Mai Apprentice

      My apology for posting the following Persistent/Hibernate question on this board. I had posted it on the Persistent/Hibernate forum but found myself visit this forum more frequent than others.

      The question is: How does an ID declared as @Id @GeneratedValue(strategy=GenerationType.AUTO) get generated?

      Say I have a simple entity with 2 attributes id and message. I don't want to worry about generating an unique ID for this record so I think would like to declare attribute id with @Id @GeneratedValue(strategy=GenerationType.AUTO).

      Can I create a new object of this class with just the message and let Hibernate create the id? In the database end, do I create the table for this entity with column id declared as bigint?

      Thanks in advance for your help.

        • 1. Re: How to use @Id @GeneratedValue(strategy=GenerationType.A
          Hung Tang Apprentice

           

          "tonylmai" wrote:


          Can I create a new object of this class with just the message and let Hibernate create the id?

          Yes
          "tonylmai" wrote:

          In the database end, do I create the table for this entity with column id declared as bigint?

          Hibernate does this automatically for you if you set "hibernate.hbm2ddl.auto" property to "create"

          • 2. Re: How to use @Id @GeneratedValue(strategy=GenerationType.A
            Richard Teviotdale Newbie

            In Enterprise JavaBeans 3.0 (O'Reilly 2006) it states "The AUTO strategy tells the persistence provider that you are allowing it to generate the key for you" (page 94).

            I don't specifically know the Hibernate implements this feature, but might I suggest that you utilize the features of your database to create primary keys instead.

            With Postgresql you could use a mapping like this:

            @Id
             @Column(name = "id", unique = true, nullable = false)
             @SequenceGenerator(name="identifier", sequenceName="ppl_id_seq", allocationSize=1)
             @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="identifier")
             @NotNull
             public int getId()
             {
             return this.id;
             }


            In this manner Hibernate would assign primary keys from the Postgresql sequence rather than internally. This would allow Hibernate to "play nicely" with other database users.

            • 3. Re: How to use @Id @GeneratedValue(strategy=GenerationType.A
              Tony Mai Apprentice

              It turned out that the @Id @GeneratedValue(strategy=GenerationType.AUTO) worked just fine.

              The problem with my code was that I had a constructor that takes in the message.

              In order for the id to be generated by the Persistent layer, I needed to construct the object using the default constructor and then used the setter to set the message attribute.

              Thanks for the help.

              Regards,
              Tony