1 2 Previous Next 16 Replies Latest reply on Mar 18, 2003 8:03 AM by Nicolás Dijkstra

    Implementing Auto generated PK

    Nuno Maia Newbie

      I'm trying to implement in Jboss 3.2 Beta auto generated PK. I already saw the post http://www.jboss.org/modules/bb/index.html?module=bb&op=viewtopic&t=forums/ but jboss give a lot of errors when I inserted the auto generated PK. A looked at the Jboss source test suite and JBoss.3.0TemplateAndExamples.zip to see if there was a sample and noting. If anyone was accomplished that could send a sample.

      Thanks a Lot

      Nuno Maia

        • 1. Re: Implementing Auto generated PK
          Alexey Loubyansky Master

          To make sure the jboss version you use supports generated by db pk values, check jbosscmp-jdbc_3_0.dtd. There should be unknown-pk element in entity element.
          If there is and you tried to configure it with the reference you mentioned and have troubles, then post the details and I'll help you.

          • 2. Re: Implementing Auto generated PK
            Nuno Maia Newbie

            Really the unknown-pk has not in jboss 3.2 beta that I had downloaded from sourceforge.net. Since I having some trouble compiling the CVS version of Jboss. There is any Jboss binary version that I can download that supports unknown-pk.


            Thanks again for you time

            Nuno Maia

            • 3. Re: Implementing Auto generated PK
              Rupinder Newbie

              i too have a similar requirement.

              i am using JBoss 3.0.3 integrated with Tomcat4.1.12 and my database is Oracle 8.1.7
              i need to know can i use the Sequence of Oracle as we can do in Weblogic for automatic primary key generation for CMP's.

              i checked the jbosscmp-jdbc_3_0.dtd that was provided with this downloaded version of JBoss.There is no unknown-pk element in this dtd.Does this mean automatic key generation cannot be used with JBoss 3.0.3 ?
              With what version of Jboss can i use this feature with Oracle?

              thanks,
              Rupinder

              • 4. Re: Implementing Auto generated PK
                Bernd Koecke Novice

                With a cvs-checkout of module 'jboss-3.2' and Tag 'Branch_3_2' you can compile a JBoss-Version which uses unknown primary keys. I don't had any problems compiling the source. I was really impressed that it works out of the box (cvs). And I have the unknown-pk element in jbosscmp-jdbc_3_0.dtd after checkout.

                In my test environment the jbosscmp-jdbc-file was build like the example in the above noted thread. My Test-DBMS is PostgreSQL, but I use a normal SQL-Command for getting the PK. The only requirement is, that the PK could be fetched before the row is written to the DB (e.g. with a sequence). I think it is not possible to use a PK which was generated by the DB at Insertion. Because you can't fetch the PK-column after the insert to fill the bean PK-field.

                I don't know if the unknownPK feature will be backported to JBoss 3.0.x, I'm not a JBoss-Developer. I think this feature was first introduced by Alex Loubyansky for JBoss 3.2.

                Bernd

                • 5. Re: Implementing Auto generated PK
                  Nuno Maia Newbie

                  I had compiled Jboss, thanks for you support. But I have one more question.

                  The deployment does't give more errors. But when I create a new bean is gives a error.

                  The jbosscmp-jdbc.xml bean.


                  <entity>
                  <ejb-name>Lixo</ejb-name>
                  <table-name>lixo</table-name>


                  <cmp-field>
                  <field-name>name</field-name>
                  <column-name>name</column-name>
                  </cmp-field>

                  <unknown-pk>
                  <unknown-pk-class>java.lang.Long</unknown-pk-class>
                  <field-name>id</field-name>
                  <column-name>id</column-name>
                  </unknown-pk>

                  <entity-command name="pk-sql">
                  <attribute name="pk-sql">select nextval ('lixo_id_seq');</attribute>
                  </entity-command>

                  </entity>


                  The bean code

                  public Long ejbCreate(Long id, String name) throws CreateException {
                  setId(id);
                  setName(name);
                  return id;
                  }

                  public void ejbPostCreate(Long id, String name) {
                  }


                  When I call the ejcCreate with a Long that Long is assigned to the bean Instead of sql sequence, if the id is called with a null value it gives a null error. Every time that bean is created the Postgresql is incrementing the sequence

                  Thanks

                  • 6. Re: Implementing Auto generated PK
                    Alexey Loubyansky Master

                    You don't have to call setId() in this case at all.
                    Actually, I think setId() shouldn't affect the correct bean creation. Do you have id declared as a cmp-field in ejb-jar.xml? If yes, then remove it. It's unknown primary key.

                    • 7. Re: Implementing Auto generated PK
                      Alexey Loubyansky Master

                      And no abstract accessors for id too.

                      • 8. Re: Implementing Auto generated PK
                        Nuno Maia Newbie

                        I had already done that and I got another error. But the sequence continues to be incremented.

                        The trace


                        1:11:14,145 ERROR [Lixo] Error checking if entity exists
                        java.sql.SQLException: ERROR: parser: parse error at or near ""

                        at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:94)
                        at org.postgresql.Connection.ExecSQL(Connection.java:398)
                        at org.postgresql.jdbc2.Statement.execute(Statement.java:130)
                        at org.postgresql.jdbc2.Statement.executeQuery(Statement.java:54)
                        at org.postgresql.jdbc2.PreparedStatement.executeQuery(PreparedStatement.java:99)
                        at org.jboss.resource.adapter.jdbc.local.LocalPreparedStatement.executeQuery(LocalPreparedStatement.java:289)
                        at org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.entityExists(JDBCCreateEntityCommand.java:168)
                        at org.jboss.ejb.plugins.cmp.jdbc.JDBCPkSqlCreateCommand.execute(JDBCPkSqlCreateCommand.java:241)
                        at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDB


                        • 9. Re: Implementing Auto generated PK
                          Alexey Loubyansky Master

                          Verify the following:
                          ejb-jar.xml
                          - prim-key-class is java.lang.Object
                          - no cmp-field for id
                          - no primkey-field

                          jbosscmp-jdbc.xml looks ok.

                          ejbCreate method should return java.lang.Object;
                          findByPrimaryKey should take the param of type Object.
                          Though, as to the last two, you can try to set other types. I am not sure though. But according to the spec, it should be Object.

                          • 10. Re: Implementing Auto generated PK
                            Bernd Koecke Novice

                            You asked for my config, so here it is. Look at the part for the PersonCmp-Bean, its the smallest one.

                            I use the name of the upk field only in container managed relationships. As Alex noted I have no field definition and no accessor methods for the upk field and I did all the other things Alex mentioned in his posting.

                            There are two differences between your and my SQL statement. I don't have a ';' at the end and it seems that you have a space between 'nextval' and '('. I think thats why you get this exception with an empty string.

                            Bernd

                            • 11. Re: Implementing Auto generated PK
                              Nuno Maia Newbie

                              It worked. Thanks to all who helped me.
                              And I have a final question and only answer if you want. But it's possible to know the last auto genereted PK.


                              Thanks to all
                              Nuno Maia

                              • 12. Re: Implementing Auto generated PK
                                Bernd Koecke Novice

                                You could do "select currval('seqname')" (Chapter 4.11 of PostgreSQL 7.2 user manual). Then you will get the last given value of the sequence, but it is only defined after a call to 'nextval' in the same process/transaction and you will get the generated and used value.

                                Another solution is to call 'select last_value from seqname', but then you don't know for which entity it was used.

                                But in an EJB-System you could call getPrimaryKey() on the local or remote interface and cast it to your declared type.

                                Bernd

                                • 13. Re: Implementing Auto generated PK
                                  Rupinder Newbie

                                  Hi,

                                  i am unable to do cvs-checkout of jboss-3.2.0 because of firewall issues.
                                  I need to know will it work if i just get the jbosscmp-jdbc_3_0.dtd having unknown-pk and replace the dtd i have in the downloaded version(which is not a cvs-checkout).
                                  If yes, can somebody provide me this dtd.

                                  thanks
                                  Rupinder

                                  • 14. Re: Implementing Auto generated PK
                                    Rupinder Newbie

                                    Hi,

                                    i am unable to do cvs-checkout of jboss-3.2.0 because of firewall issues.
                                    I need to know will it work if i just get the jbosscmp-jdbc_3_0.dtd having unknown-pk and replace the dtd i have in the downloaded version(which is not a cvs-checkout).
                                    If yes, can somebody provide me this dtd.

                                    thanks
                                    Rupinder

                                    1 2 Previous Next