1 2 Previous Next 24 Replies Latest reply on Jun 18, 2004 3:26 AM by Alexey Loubyansky Go to original post
      • 15. Re: Unknown-PK Problems...
        sesques Novice

        Hi hpx,

        I confess having no interrest on Nukes, so I cannot help you about it.
        About unknow-pk and CMR, of course unknown-pk can be used by relationships.
        I use it myself without problem, declaring unknow-pk field-name as related-pk-field on a jboss relation:

        My first entity bean PhML_Repxml declares its primary key as follows:
        (the key is auto-incremented on a Microsoft SQL 2000 database)

         * @ejb.pk class = "java.lang.Object"
         * @jboss.unknown-pk
         * class="java.lang.Long"
         * column-name="REPXML_ID"
         * field-name="repxmlId"
         * jdbc-type="NUMERIC"
         * sql-type="NUMERIC"
         * auto-increment="true"
         * @jboss.entity-command name = "mssql-fetch-key"

        and the relation on the other entity bean is declared as follows:
         * @ejb:relation
         * name="ReqXml-RepXml"
         * role-name="ReqXml-have-RepXml"
         * target-ejb="PhML_Repxml"
         * @jboss:relation
         * fk-column = "REPXML_ID"
         * related-pk-field = "repxmlId"

        It's working fine. There is no bug or peculiarity about XDoclet.
        Look attentively at your descriptors, eventually post it if you are in a dead end.

        • 16. Re: Unknown-PK Problems...
          Michael Forster Newbie

          With MySQL you can have AutoIncrement Primary Key Fields without using Unknown-PK's

          BUT you have to let JBoss create the Database (or make sure you create it correctly to match what JBoss is looking for.

          The key is to dec;are your PK in jbosscmp-jdbc.xml as


          and have the

          <entity-command name="mysql-get-generated-keys"

          in the entity

          we have our system set up as follows

           <entity-command name="mysql-get-generated-keys"

          our ejb-jar is as follows


          This works fine... and not an Unknown-pk in sight

          • 17. Re: Unknown-PK Problems...
            Howard Pearlmutter Novice


            "mpforste" wrote:
            With MySQL you can have AutoIncrement Primary Key Fields without using Unknown-PK's
            This works fine... and not an Unknown-pk in sight

            You seem to be kind of proud of getting rid of unknown-pk.... why? Wouldn't you consider that a step backwards?

            I appreciate your willingness to share code and your intent to be helpful. I just question whether there is any virtue in going back into database-dependence.

            "hxp" wrote:
            Unknown-PK going with XDoclet and _CMR_, such that successful build, deployment, and runtime behavior is proper with both MySQL and HSQL?

            The goal here is to create a single entity template that will work properly across all the DBMS's. That's what's exciting about unknown-pk.

            Any reasons you (or anyone else on this thread) would want to get away from unknown-pk?

            --- Howard

            • 18. Re: Unknown-PK Problems...
              Howard Pearlmutter Novice

              Sesques --

              The part of your code example that triggered me in a good direction was "target-ejb"; I had the other parts already. BTW, the main solution to the bugs I was having was removing @jboss.relation from the 1 end of the relation. So you were indeed right that xdoclet, unknown-pk, and CMR coexist without problems. So I've successfully upgraded the Nukes FAQ module to pure-CMP so that it doesnt use sql/ddl anymore at all. Thanks for your help!

              -- Howard

              • 19. Re: Unknown-PK Problems...
                Howard Pearlmutter Novice

                Sesques --

                Over in nukesland, we're having some dificulties pulling everything (CMP, PK automation, CMR, xdoclet) together so that it works equivalently and transparently across all the DBMS's (at least HSQL and MySQL).

                I'd appreciate it if you could please take a look at these threads:

                MySQL & Pure-CMP --- issues w News & FAQ modules

                Pure-CMP & FAQ Module revamp

                Thanks in advance for any insights you can offer, and thanks again for your earlier help.

                -- Howard

                • 20. Re: Unknown-PK Problems...
                  sesques Novice

                  Hi Howard,

                  I had a look and post my comment. Just for fun because the solution was already posted I think. Effectively, the datasource-mapping allows you to be bored with SQL mapping or pk-constraint considerations.
                  I did it myself, deploying the sames EJB's with several types like String Date, Integer, BigInteger etc., with relationships, with auto-increment on Hypersonic, MySQL, MS SQL2000, Oracle and all works fine. Even with unknown-pk on MySQL, and in spite of that someones do what they can to avoid this tag...

                  Very humbly, I hope that I had help you.


                  • 21. Re: Unknown-PK Problems...
                    gych Newbie

                    Due to the fact that my Oracle database is used by non-JBoss access means as well, I've set up all my tables to have an auto-increment primary key field populated by sequences via insert triggers. The database also enforces primary key immutability via these triggers and prevents someone from dropping in a primary key value out of sequence or trying to back-fill values.

                    I could have used the oracle-sequence entity command as is, letting it pull a sequence.nextval then the insert trigger doing the same, but I got an every-other-value behavior.

                    Solution: customize the JDBCOracleCreateCommand class

                    By commenting out lines 58 and 64 -- which includes the PK during the insert and pulls the nextval from the sequence -- I let the trigger do the populating but still pulled the return value out just like before for returning to the entity.

                    Seems like a pretty typical problem and has an easy solution. But I was left wondering why this behavior isn't a standard entity-command object somewhere. Isn't a post-insert PK pull pretty common?

                    • 22. Re: Unknown-PK Problems...
                      Alexey Loubyansky Master

                      Many of the commands in the keygen package pull the pk value after insert. You are welcome to patch or write and contribute your own entity-command. Thanks.

                      • 23. Re: Unknown-PK Problems...
                        Gavin Lang Newbie


                        How do you get a reference to your primary key using your method below?

                        "sesques" wrote:
                        Normally, this should work and is compliant wth the EJB spec.
                        (But if not, keep your version as well, it will be a software mystery ;-)

                         * The Employee Entity Bean handles the information of an employee.
                         * @author Karsten Jahn (OrangeOak)
                         * @version 1.0
                         * @ejb.bean
                         * name = "Employee"
                         * display-name = "EB_DataBase - Employee: Employee"
                         * description = "The Employee Entity Bean saves every Employee."
                         * view-type = "local"
                         * type = "CMP"
                         * cmp-version = "2.x"
                         * local-jndi-name = "ejb/medsched/employee/Employee"
                         * reentrant = "false"
                         * @ejb.pk class = "java.lang.Object"
                         * @jboss.persistence
                         * create-table = "true"
                         * remove-table = "false"
                         * datasource = "java:/mySQLDS"
                         * datasource-mapping = "mySQL"
                         * table-name = "employee"
                         * @jboss.unknown-pk
                         * class = "java.lang.Integer"
                         * auto-increment = "true"
                         * column-name = "employeeid"
                         * field-name="employeeID"
                         * sql-type = "INTEGER"
                         * jdbc-type = "INTEGER"
                         * @jboss.entity-command name = "mysql-get-generated-keys"
                        public abstract class EmployeeBean implements EntityBean {
                         * @throws CreateException
                         * @ejb.create-method
                         public java.lang.Object ejbCreate(StaffMember employee) throws CreateException {
                         return null;
                         public void ejbPostCreate(StaffMember employee) {

                        • 24. Re: Unknown-PK Problems...
                          Alexey Loubyansky Master

                          getPrimaryKey() on EJB[Local]Object or EntityContext.

                          1 2 Previous Next