5 Replies Latest reply on Apr 30, 2008 8:23 AM by Carsten Hoehne

    Auto Increment problems

    Tom Christensen Newbie

      I am having a bit of an issue.  first some background I guess.  I am new to Seam and its been a while (5 years) since I did much Java programming.  I am coming from python where I was using django quite extensively, so I do have recent experience with web frameworks, MVC, and the basic principles at work.  I have searched this forum, and google for the last 2 hours to no avail...


      My issue is I created a tiny database with 3 tables, a couple of foreign keys, etc.  All tables have auto increment primary key fields called id.  I am using postgresql as my DB.  I am able to deploy the application, go to localhost:8080/firsttry and see my application.  After running generate-entities on the database, I can see the lists of the objects, I can pull up a form to add new objects, etc. 


      The problem I have is that seam does not seem to recognize the auto increment fields as such.  They are displayed on the forms and they default to a value of 0.  Submitting 2 objects with 0 as the id obviously breaks, and realistically I don't think this should show up at all as an editable field, as the DB should handle this.  I tried adding @SequenceGenerator and @GeneratedValue to the id field in my model class (obviously with the attributes pointing to the sequence in postgresql, etc), but this changed nothing.


      How do you handle auto increment fields when the field gets its value from a sequence?  Both postgresql and oracle work this way, so I can't imagine this is an unknown problem.


      Thanks, and sorry for my ignorance.

        • 1. Re: Auto Increment problems
          Damian Harvey Apprentice

          And you've tried taking the field off of the form so that it is not submitted in the post?


          As you've said, auto-incrementing fields are very common and is easily handled by Seam (well more by Hibernate/JPA).


          Cheers,


          Damian.

          • 2. Re: Auto Increment problems
            Fernando Montaño Expert

            Have you tried something like:


            @Id
            @GeneratedValue(strategy=GenerationType.IDENTITY)
            privateLong id;




            This works for me in mysql autoincrement columns.


            HTH.

            • 3. Re: Auto Increment problems
              Tom Christensen Newbie

              Yes, I took the fields in question out of the form, but they still submit 0, unless I add the @SequenceGenerator and @GeneratedValue annotations.  This worked.


              However, I then added 2 more tables to the DB and ran generate-entities again, and it overwrote all of my changes (and interestingly, duplicated the fields that were still on the forms, so now I have companyName twice, firstName twice, etc).  How would one go about doing incremental development like this?  Or does Seam expect the entire DB to be designed and implemented before you start on the code?


              Is there a different script to run that will only add in the changes to the DB?  Or to do incremental dev like this, do you just have to manually create the classes/views/etc?


              As to the GenerationType.IDENTITY, that will not work in postgresql because PostgreSQL uses a sequence to populate the auto increment field.  I guess I just kinda expected Seam (or hibernate/jpa) to do a better job at discovering the DB and coming up with sensible defaults for forms.

              • 4. Re: Auto Increment problems
                Zahid Maqbool Newbie

                This is how I have done in Oracle 10g and 9i



                @Id
                @GeneratedValue(generator="afsStatus_Seq")
                @SequenceGenerator(name="afsStatus_Seq",sequenceName="AFSSTATUS_SEQUENCE", allocationSize=1)
                @Column(name="ID")
                
                    public long getId() {
                     return id;
                    }



                and this is how I have created my sequence:



                CREATE SEQUENCE  "MEWEUSER"."AFSSTATUS_SEQUENCE"  MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE  ORDER  NOCYCLE ;





                • 5. Re: Auto Increment problems
                  Carsten Hoehne Master

                  You seem to have missed this post:
                  Form Builder