6 Replies Latest reply on Nov 5, 2001 9:38 PM by Erik Sliman

    Autogenerated PK with CMP

    Erik Sliman Apprentice

      Can someone please recommend the best way to autogenerate a primary key when using CMP?

      If I have to, I am willing to do it in the database. However, if I go this route, I need to at least be sure that the ejbCreate will not interfere with the creation of the PK, and should, preferably, obtain the value generated in the database.

      I am using MySQL right now.

      Thanks,

        • 1. Re: Autogenerated PK with CMP
          Erik Sliman Apprentice

          I took care of the MySQL part of autogenerating, so that so long as I insert with a null in the PK column, it will generate a number for me.

          Now I cannot get CMP to handle it correctly. If I give it a specific value greater than 0, then it inserts ok, however this defeauts autogeneration.

          If I give it 0 or less, then I can see from the MySQL log that it inserts the value as 'null', yet rolls back. The SQL is valid, as I can run it direclty into MySQL. So, it isn't MySQL that's the problem.

          The error I get is "Error: id may not be null".

          I don't understand why it converts 0 or less to null. Not that I'm complaining about this, because that is ironically the behavior I want.

          This kinda worked the first run before I started making changes. It would autogenerate a number, effectively ignoring any number I entered. I cannot get it back to this state, although this state has obvious problems as well.

          Lastly, assuming that you are able to help me get it to send a null to the database, and be happy. How do I then get the value generated from the database? It does not seem to requery.

          • 2. Re: Autogenerated PK with CMP
            Andreas Schaefer Apprentice

            I solved it this way:
            - Created a Stateless Session Bean having a method "int createNumber( String pTable )" where pTable is the name of the table
            - in the ejbCreate() the number is retrieved from the SLSB and set into the ID
            - the SLSB which handles the business logic of the entites checks if the ID is null, if yes then it will create the Entity Bean otherwise use a finder by primary key to look it up

            The SLSB delivering the PK number hides the logic for your application and DB how to create the PK. This way you can easily adapt to a new DB or other chances because the Entity Bean does not know how to create the PK - you just have to adapt the SLSB.

            Have fun - Andy

            • 3. Re: Autogenerated PK with CMP
              Erik Sliman Apprentice

              I got it to the point, where it would add once with PK of 0, which got replaced in the DBMS with a generated number > 0. The second time you added, it would create an error. If you tried to add the same entry again, it would succeed.

              I suspect that the first time you create, it thinks the PK is 0, even though it is changed by the DBMS. The second time you create, it sees there is already an EJB with a PK of 0, and creates an error. However, at the same time, it must also realize there is no 0 in the database, causing it to terminate the EJB. Thus, the next time you try, it succeeds.

              The work-around is hoky and has redundant code, but looks like this:

              try {
              Application newApplication =
              applicationHome.create(id, name, description );

              // Enter EDIT mode
              iMode = modeEdit;
              bAction = true;
              id = newApplication.getId();
              } catch (Exception ignoreex) { //ex3b
              try {
              Application newApplication =
              applicationHome.create(id, name, description );

              // Enter EDIT mode
              iMode = modeEdit;
              bAction = true;
              id = newApplication.getId();
              } catch (Exception ex) { //ex3c
              strError = "Caught an exception while adding new item for " + id + ": " + ex.getMessage();
              lResult = ErrGeneral;
              }
              }

              • 4. Re: Autogenerated PK with CMP
                Andreas Schaefer Apprentice

                Can post or send me the code of the Application Bean class ?

                Thanx - Andy

                • 5. Re: Autogenerated PK with CMP
                  Andreas Schaefer Apprentice

                  Your problem is that you do NOT create a primary key number for the Entity.

                  It is the responsibility of the Entity Bean to set the value(s) of the primary key. Normally this is done in the ejbCreate() method. Therefore the code would look like this:

                  public Integer ejbCreate (Integer _id, String _name, String _description)
                  {
                  if( _id == null ) {
                  // Create the Primary Key Value
                  _id = createPrimaryKey();
                  }
                  setId(_id);
                  setName(_name);
                  setDescription(_description);
                  return null;
                  }

                  ----------------------------------------------------

                  Have fun - Andy

                  • 6. Re: Autogenerated PK with CMP
                    Erik Sliman Apprentice

                    I realize this is the ideal solutions, and plan to someday do it. I'm not looking forward to it, because it will require more beans and at least one more MySQL table, as well as testing, etc. I've seen lots of discussions on it, but not 100% working code I can just add and use.

                    In the meantime, my workaround seems to be working great. Thanks.