6 Replies Latest reply on May 18, 2005 9:53 AM by qwidjibo

    autokey generation (3.2.5 + hypersonic)

    gioele

      I can't get hypersonic to generate primary keys for me in an automated way.

      This is what I have in jbosscmp-jdbc.xml

      <entity>
       <ejb-name>StudentGroup</ejb-name>
      
       <cmp-field>
       <field-name>id</field-name>
       <auto-increment/>
       </cmp-field>
      
       <cmp-field>
       <field-name>password</field-name>
       </cmp-field>
      
       <entity-command name="get-generated-keys">
      </entity-command>


      in ejb-jar.xml (some fields skipped)

      <entity>
       <ejb-name>StudentGroup</ejb-name>
       <persistence-type>Container</persistence-type>
       <prim-key-class>java.lang.Integer</prim-key-class>
       <reentrant>False</reentrant>
       <cmp-version>2.x</cmp-version>
       <abstract-schema-name>StudentGroup</abstract-schema-name>
      
       <cmp-field >
       <field-name>id</field-name>
       </cmp-field>
      
       <cmp-field >
       <field-name>password</field-name>
       </cmp-field>
      
       <primkey-field>id</primkey-field>
      </entity>


      Everytime I create a new StudentGroup I get a null value for getId (both in ejbCreate() and in ejbPostCreate). Obviously the DB ends up corrupted.

      What is the correct way to get the autogenerated key?

      I thought that with autogenerated primary keys all I had to do was to create a simple ejbCreate that ends with return getId();, being the id field already set by the DB. It seems that I am wrong.


        • 1. Re: autokey generation (3.2.5 + hypersonic)
          darranl

          I have never tried getting auto-increment to work with Hypersonic so can't comment on your specific problem.

          However the return value from the ejbCreate method in a CMP entity bean should always be null, the container handles the rest.

          • 2. Re: autokey generation (3.2.5 + hypersonic)
            gioele

            Now my ejbCreate end with return null, but the problems are still there...

            Can someone post examples of working setups?

            • 3. Re: autokey generation (3.2.5 + hypersonic)
              gioele

              The problem was that jbosscmp-jdbc.xml was not included in the correct jar file. Now the problem seems solved.

              • 4. Re: autokey generation (3.2.5 + hypersonic)
                qwidjibo

                Hi,

                I'm trying to get auto increment to work with hypersonic db and have read many articles but there aren't any solid examples to follow.
                I'm really new to JBoss and only using it for a uni coursework, so I'm on a tight schedule.

                Most of the examples include dropping <cmp-field> from the ejb-jar.xml and using the <pk-unknown> element in the jbosscmp-jdbc.xml file.

                Your example seems to have just added <auto-increment/> to the jbosscmp-jdbc.xml file, does this work? If so, I'd appreciate it if you could post a copy of the working xml files.

                Cheers

                • 5. Re: autokey generation (3.2.5 + hypersonic)
                  gioele

                   

                  "qwidjibo" wrote:
                  Your example seems to have just added <auto-increment/> to the jbosscmp-jdbc.xml file, does this work? If so, I'd appreciate it if you could post a copy of the working xml files.

                  Yes, that's almost all I've to do to get auto pk generation.
                  I used these two xdoclet tags to the class comments:

                  @jboss.persistence pk-constraint="false"
                  @jboss.entity-command name="hsqldb-fetch-key"
                  


                  and this one to the getter method of the pk

                  @jboss.persistence auto-increment="true"
                  


                  That's all. (remember to include all the deployment descriptors in the correct archives)


                  • 6. Re: autokey generation (3.2.5 + hypersonic)
                    qwidjibo

                    Cheers,

                    I'm not using Xdoclet at the moment, just trying to plough my way through and get to grips with everything before I start to take short cuts(I'm supposed to be evaluating EJB2.x persistence against hibernates persistence mechanisms).

                    I presume the tags add the necessary elements to the xml files, I'll have to check what alterations the tags cause.

                    Thanks again