4 Replies Latest reply on Oct 28, 2003 5:44 PM by hildo

    Easiest way to configure container to auto-increment key fie

    hildo

      I'm writing a J2EE application, which will be using CMP Entity Beans. I'd like the entity beans to use an Integer field as their key field. I've added some code in my application to generate a valid unused key, which can be retrieved during the ejbCreate method and used as the key for the newly-created object.

      My question is: can the CMP Container be configured so it can automatically create a new key, so my EJB Application doesn't have to query the database each time ejbCreate is called?

      I'm using the default JBoss datasource (HyperSonic).
      I've seen something in the jbosscmp-jdbc deployment descriptor about an "auto-increment" element, but I can't find any documentation about how to use it.

      Thanks for any help,
      Ed

        • 1. Re: Easiest way to configure container to auto-increment key
          hildo

          In case anyone is watching, I found some documentation for Hypersonic. They allow a special column, called the IDENTITY column. When calling insert on a table which has been created with this column (either as it's own type, or as the default syntax CREATE TABLE <table name> <column name> INTEGER INDENTITY,...) any null value for the column will result in an auto-generated value for the column.

          I haven't tried this, but this looks like an alternative to any local generation logic in the Bean or the Container.

          Thanks,
          Ed

          • 2. Re: Easiest way to configure container to auto-increment key
            afschell

            I'm curious to see if this works for you. How are you mapping the bean's attribute to the IDENTIY column? I'm thinking that needs to be done so that the bean's findByPrimaryKey methods function correctly.

            Please let me know...afschell

            • 3. Re: Easiest way to configure container to auto-increment key
              jcordes

              Researching on "hypersonic entity-command" brought forth
              http://www.jboss.org/modules/bb/index.html?module=bb&op=viewtopic&t= it's not in JBoss 3.2.2, just apply the patch for yourself.

              So basically adding a and <entity-command name="hsqldb-fetch-key"/> to your jbosscmp-jdbc.xml at the appropiate place (have a look at the dtd) for each entity with auto-generated id is all there's to it. I would rather recommend to use XDoclet for generating deployment descriptors (editing by hand could be getting annoying sometimes ;-)).

              HTH,

              Jochen.

              • 4. Re: Easiest way to configure container to auto-increment key
                hildo

                Multiple replies... :)

                afschell (I probably misspelled that).. I haven't attempted to name my column to Identity, so I can't confirm or deny that this works.

                Reading jcordes link, the hsqldb-fetcch-key calls the INDENTY() method, which appears to do the same thing.

                I haven't tried the tag as yet, because I think I need to have a jboss.xml file, and the version that xdoclet generates doesn't seem to work with the version of JBoss I'm using (4.0 ... yes, I'm naughty).

                Another annoyance, is that XDoclet doesn't seem to create the CMP part in the ejb-jar.xml either... it leaves out the primary key field, which results in an error when creating the CMP tables. So, I use xdoclet to create the descriptors when I need lots of new entries (like finder methods or relationship definitions) and then copy them into a source which I then use to build into my jar file. Very painful. If nobody picks up on the bug, I may have to look into fixing it myself. ;)

                Thanks all!