1 2 3 Previous Next 32 Replies Latest reply on Jan 27, 2003 6:30 AM by Simone

    NEW FEATURE: new ways to create entity beans

    Alexey Loubyansky Master

      Hello folks!

      JBossCMP now provides some new ways to setup and create entity beans.
      It is based on so called entity commands.
      First some working examples (snippet from jbosscmp-jdbc.xml):

      <ejb-name>MyCMPBean</ejb-name>
      <table-name>mycmp_table</table-name>

      <!-- auto-incremented primary key (jdk1.3/1.4) for now supported only for MySQL. -->
      <cmp-field>
      <field-name>id</field-name>
      <column-name>knownid</column-name>
      <jdbc-type>INTEGER</jdbc-type>
      <sql-type>INTEGER(11)</sql-type>
      <auto-increment/>
      </cmp-field>
      <!-- entity command to create the entity -->
      <entity-command name="mysql-get-generated-keys"/>

      <!-- unknown primary key generated with SQL statement -->
      <unknown-pk>
      <unknown-pk-class>java.lang.Integer</unknown-pk-class>
      <column-name>genid</column-name>
      <jdbc-type>TIMESTAMP</jdbc-type>
      <sql-type>DATETIME</sql-type>
      </unknown-pk>
      <entity-command name="pk-sql"/>
      -->
      <!-- pk-sql generated unknown-pk; overriding default sql
      <unknown-pk>
      <unknown-pk-class>java.lang.Integer</unknown-pk-class>
      <column-name>genid</column-name>
      <jdbc-type>INTEGER</jdbc-type>
      <sql-type>INTEGER</sql-type>
      </unknown-pk>
      <entity-command name="pk-sql">
      SELECT SEQ_2279_1.nextval FROM DUAL
      </entity-command>
      -->
      <!-- mysql generated unknown-pk
      <unknown-pk>
      <unknown-pk-class>java.lang.Integer</unknown-pk-class>
      <column-name>genid</column-name>
      <jdbc-type>INTEGER</jdbc-type>
      <sql-type>INTEGER(11)</sql-type>
      <auto-increment/>
      </unknown-pk>
      <entity-command name="mysql-get-generated-keys"
      class="org.jboss.ejb.plugins.cmp.jdbc.mysql.MySQLCreateEntityCommand"/>
      -->
      <!-- the use of key generator for unknown-pk
      <unknown-pk>
      <unknown-pk-class>java.lang.String</unknown-pk-class>
      <column-name>genid</column-name>
      <jdbc-type>VARCHAR</jdbc-type>
      <sql-type>VARCHAR(32)</sql-type>
      </unknown-pk>
      <entity-command name="key-generator"
      class="org.jboss.ejb.plugins.cmp.jdbc.JDBCKeyGeneratorCreateCommand">
      UUIDKeyGeneratorFactory
      </entity-command>
      -->


      The general syntax of entity-command is:
      <entity-command name="CommandName"
      class="ComandClass">
      value1
      value2
      </entity-command>

      Only the 'name' is required.
      You can check the list of available commands in
      standardjbosscmp-jdbc.xml in entity-commands element.
      The default entity command is specified in 'defaults' element and can
      be overriden in each entity element as above.
      If entity-command in the entity provides 'class' or 'attribute' it
      overrides the corresponding element of in the known command.

      For now the feature is available in HEAD version.

      PS: templates for non-MySQL auto-increment SQL statements are welcome!!!

      alex

        • 1. Re: NEW FEATURE: new ways to create entity beans
          Alexey Loubyansky Master

          This is now ported to Branch_3_2.

          alex

          • 2. Re: NEW FEATURE: new ways to create entity beans
            Roger Newbie

            This looks great. Is there a PostgreSQL 7.2 version planned in the near future please?

            Roger

            • 3. Re: NEW FEATURE: new ways to create entity beans
              Alexey Loubyansky Master

              Commands that make use of key generators or custom sql query that fetches the next value for pk can be used with PostgreSQL right now.
              As to key generators, only UUIDKeyGenerator is now available.

              If you'd like to get primary keys generated by PostgreSQL, please, provide a corresponding create-table sql statement.

              alex

              • 4. Re: NEW FEATURE: new ways to create entity beans
                Roger Newbie

                Thank you very much Alex for your prompt and helpful reply. Please forgive my ignorance in these next couple of questions. Are "key generators" and "custom sql" restricted to BMP only? If yes to this first question can EJB QL be used instead of custom sql to achieve the same effect? What is UUIDKeyGenerator and where can I found out how to use it, please? When you say, "provide a corresponding create-table sql statement" do you mean something like:

                CREATE TABLE network (
                net01_network_id SERIAL CONSTRAINT network_pk PRIMARY KEY,
                net01_network_name CHAR(150),
                net01_network_description CHAR(800),
                );

                Thanks again,
                Roger

                • 5. Re: NEW FEATURE: new ways to create entity beans
                  Alexey Loubyansky Master

                  >Are "key generators" and "custom sql" restricted to BMP only?
                  Not at all! CMP.

                  >What is UUIDKeyGenerator and where can I found out how to use it, please?

                  There are two notions: KeyGenerator and KeyGeneratorFactory. KeyGenerator produces unique keys. KeyGeneratorFactory produces key generators.

                  For now only UUIDKeyGenerator and UUIDKeyGeneratorFactory are available. They are shipped with JBoss and are in 'default' and 'all' configurations (uuid-key-generator.sar).
                  UUIDKeyGeneratorFactory is bound in global JNDI with this same name.
                  All you need is to specify the JNDI name of the factory as the value for the element , as shown above. The container will do everything else.
                  KeyGenerator is created per each entity.

                  If someone would like a how-to on implementating other KeyGenerators/KeyGeneratorFactories, please, ask or see the source.

                  Thank you for the sql statement. It's what I needed. If PostgreSQL's api allows to fetch generated keys, we'll support it soon. If someone wishes to implement it, welcome. For now I even don't have PostgreSQL installed.

                  alex

                  • 6. Re: NEW FEATURE: new ways to create entity beans
                    Roger Newbie

                    Thanks Alex for this info.

                    Unfortunately, I am unable to attempt implementing KeyGenerators for PostgreSQL at present. However, I think the PostgreSQL API supports the functionality necessary to create a KeyGenerator for PostgreSQL. The SERIAL command in the above CREATE statement automatically creates a sequence that is used for auto-key generation. The manual states that the name of the automatically generated sequence has the form tablename_columnname_seq. However, sometimes an extra letter is inserted before the _seq, probably to avoid the duplication of names. The sequence can be accessed via three functions, nextval(), currval() and setval(), which return the next value, the current value and set the counter to a new value, respectively. These functions are placed in a SELECT statement. For example: SELECT nextval(sequence_name). Note there is a BIGSERIAL for numbers greater than 2 to the power fo 31. The auto-key generation can be set up more explicitly using sequences explicitly without using the SERIAL command. A helpful and easy to read introduction to PostgreSQL is "PostgreSQL Introduction and Concepts" by Bruce Momjian. It may still be available free from the PostgreSQL web site.

                    I have two questions please. What version of JBoss is the UUIDKeyGenerator available in? And what version is the <entity-command> tag available in? I am currently running version 3.0.2, but could not see the file "uuid-key-generator.sar" anywhere in my directories.

                    Regards,
                    Roger

                    • 7. Re: NEW FEATURE: new ways to create entity beans
                      Alexey Loubyansky Master

                      Thank you very very much.
                      The stuff is in Branch_3_2 and HEAD.
                      Also Branch_3_2 is more stable then Branch_3_0 and has many bugs fixed.

                      • 8. Re: NEW FEATURE: new ways to create entity beans
                        Bernd Koecke Novice

                        Hi,

                        many thanks for the new feature! It is a key feature for accessing existing databases with CMP.

                        I have a little question about the fieldname of the unknown primary key (upk). In my testsystem is the upk used in a relationship. Bean A has a column with the pk of Bean B and B has the upk. For this I have to map the upk-field of B to the column-name of A. The current implementation generates the name of the upk-field like this: 'BeanName' + '_upk'. Will this be the same in the future? The name is needed in the key-field tag in the ejb-relationship-role in jbosscmp-jdbc.xml.

                        Another solution would be to check the field-name tag in the unknown-pk section of jbosscmp-jdbc.xml. Than it would be possible to overwrite the generated name of the upk-field.

                        I think it would be a nice feature if it is possible to overwrite the generated name. But if the generation rule doesn't change it is not necessary. Or do I miss something and it is still possible?

                        Thanks

                        Bernd

                        • 9. Re: NEW FEATURE: new ways to create entity beans
                          Bernd Koecke Novice

                          Hi,

                          many thanks for the new feature! It is a key feature for accessing existing databases with CMP.

                          I have a little question about the fieldname of the unknown primary key (upk). In my testsystem is the upk used in a relationship. Bean A has a column with the pk of Bean B and B has the upk. For this I have to map the upk-field of B to the column-name of A. The current implementation generates the name of the upk-field like this: 'BeanName' + '_upk'. Will this be the same in the future? The name is needed in the key-field tag in the ejb-relationship-role in jbosscmp-jdbc.xml.

                          Another solution would be to check the field-name tag in the unknown-pk section of jbosscmp-jdbc.xml. Than it would be possible to overwrite the generated name of the upk-field.

                          I think it would be a nice feature if it is possible to overwrite the generated name. But if the generation rule doesn't change it is not necessary. Or do I miss something and it is still possible?

                          Thanks

                          Bernd

                          P.S.: Sorry if you get this posted twice, but my I can't see my first posting.

                          • 10. Re: NEW FEATURE: new ways to create entity beans
                            Alexey Loubyansky Master

                            Having
                            <unknown-pk>
                            <unknown-pk-class>java.lang.Integer</unknown-pk-class>
                            <column-name>genid</column-name>
                            <jdbc-type>TIMESTAMP</jdbc-type>
                            <sql-type>DATETIME</sql-type>
                            </unknown-pk>
                            I get field in the db named as 'genid'. Doesn't it work for you?

                            • 11. Re: NEW FEATURE: new ways to create entity beans
                              Bernd Koecke Novice

                              No, I think that doesn't work.

                              I have two tables:
                              Table person with a column 'addressid' and address with a column 'id'. The id-column in address is generated by a sequence in PostreSQL. This id will be filled into the column 'addressid' in the person table. In jbosscmp-jdbc.xml is the following xml-part:

                              <ejb-relation>
                              <ejb-relation-name>PersonCmp-AddressCmp</ejb-relation-name>
                              <read-only>false</read-only>
                              <read-time-out>500</read-time-out>
                              <foreign-key-mapping/>
                              <ejb-relationship-role>
                              <ejb-relationship-role-name>person-has-address</ejb-relationship-role-name>
                              <key-fields/>
                              <read-ahead>
                              none
                              </read-ahead>
                              </ejb-relationship-role>
                              <ejb-relationship-role>
                              <ejb-relationship-role-name>address-for-person</ejb-relationship-role-name>
                              <key-fields>
                              <key-field>
                              <field-name>AddressCmpEJB_upk</field-name>
                              <column-name>addressid</column-name>
                              </key-field>
                              </key-fields>
                              <read-ahead>
                              none
                              </read-ahead>
                              </ejb-relationship-role>
                              </ejb-relation>


                              The field of the Address-Bean must be mapped to the column in the person-table. When I try to do it the other way round, mapping the column 'id' to the field 'address' in Person-Bean, then JBoss complaines, because he needs a cmp-field and address is a cmr-field. I think I need the name of the primary key field. That's no problem if the rule for generating this fieldname is stable or if I could optionally overwrite this name.

                              Thanks

                              Bernd

                              • 12. Re: NEW FEATURE: new ways to create entity beans
                                Alexey Loubyansky Master

                                You are right. I've just fixed it in Branch_3_2. Use field-name element to override the default "_upk".
                                <unknown-pk>
                                <unknown-pk-class>java.lang.String</unknown-pk-class>
                                <field-name>genfield</field-name>
                                <column-name>genid</column-name>
                                <jdbc-type>VARCHAR</jdbc-type>
                                <sql-type>VARCHAR(32)</sql-type>
                                </unknown-pk>
                                Thanks!

                                • 13. Re: NEW FEATURE: new ways to create entity beans
                                  Bernd Koecke Novice

                                  Hi Alex,

                                  thanks a lot! It works fine. Now its much easier to explain how a jbosscmp-jdbc-file has to look like and why.

                                  Bernd

                                  • 14. Re: NEW FEATURE: new ways to create entity beans
                                    Brian Hale Newbie

                                    Is this capability available in the jboss-3.0.3_tomcat-4.1.12 bundle? If not are there plans to port the functionality into another tomcat bundle? Is there any place that I can get an idea of the release schedule? I have been pounding on this for quite a long time, and I have not been able to find the answer to my questions.

                                    1 2 3 Previous Next