4 Replies Latest reply on Sep 13, 2007 2:44 PM by rjstanford

    Getting generated ID by sequence

    stupar.aleksandar

      I'm writing entity for person and this is the part of the class.

      @Entity
      @SequenceGenerator(name = "tsperson_sequence", sequenceName = "SEQ_TSPERSON_ID")
      public class Tsperson implements Serializable {
       @Id
       @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="tsperson_sequence")
       @Column(name="TSPERSON_ID", nullable = false)
       private Long tspersonId;


      What i'm trying to do is to get an Id after I flush person data
      into database.

      Tsperson person = new Tsperson();
       person.setFirstName("kill");
       person.setLastName("em");
       person.setUserName("all");
      
       em.persist(person);
       System.out.println(person.getTspersonId());
       em.flush();
       System.out.println(person.getTspersonId());


      But all I get is old
      stohastic Long value generated by container.

      Output is something like this:
      3150
      3150
      and primary key in database is 60.

      Could someone tell my is this normal and how can I
      get value generated from database sequence
      into Person's memory.

      Thnx in front.

        • 1. Re: Getting generated ID by sequence
          rabbiaqaswar

          try:

          @Entity
          public class Tsperson implements Serializable {
          @Id
          @Column(name="TSPERSON_ID", nullable = false)
          @SequenceGenerator(name = "tsperson_sequence", sequenceName = "SEQ_TSPERSON_ID")
          @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="tsperson_sequence")
          private Long tspersonId;
          


          why do you need to flush data?

          • 2. Re: Getting generated ID by sequence
            andydale

            Hi,

            Try declaring your sdequence as so:

            @SequenceGenerator(name = "tsperson_sequence", allocationSize=1, sequenceName = "SEQ_TSPERSON_ID")

            You must have the allocationSize=1 (or some other value) or it uses some kind of hi-lo strategy.

            Cheers,

            Andy

            • 3. Re: Getting generated ID by sequence
              stupar.aleksandar

               

              "andydale" wrote:
              Hi,

              Try declaring your sdequence as so:
              @SequenceGenerator(name = "tsperson_sequence", allocationSize=1, sequenceName = "SEQ_TSPERSON_ID")

              You must have the allocationSize=1 (or some other value) or it uses some kind of hi-lo strategy.

              Cheers,

              Andy


              When I declare SequenceGenerator with allocationSize
              id of object in memory is always id in DB minus one.
              Do you know what the problem is, and how can I solve it.

              Thnx

              • 4. Re: Getting generated ID by sequence

                I'm having a similar problem - using MySQL as the backend (not that I think it matters) and just the default @GeneratedValue option.

                I've seen references in other sites that imply that if you use a generated value, the Id will be updated as soon as you call persist(). In our case, before calling persist() it is null (correct), but after calling persist() its always zero.

                I've tried doing a refresh() afterwards, but that doesn't seem to make a difference either.

                To avoid the questions, we're trying to get the Id of a newly created (and inserted) object so that we can push that Id into Session state. Seems simple enough, right?

                -Richard