EJB m:n relationship in JBoss + Xdoclet javacode 9085791

EJB m:n relationship in JBoss + Xdoclet

As a (promised) follow-up on the uni-direcional CMP/CMR example in EJB CMP/CMR example with JBoss+Xdoclet, we present a many-to-many (m:n) relationship example here, albeit in much less detail. For more details the reader is referred to the above post.

A many-to-many entity bean relationship

In EJB CMP/CMR example with JBoss+Xdoclet, there was an example of a bibliography, that may contain articles, books, etc. Now a book/article can be written by one or more authors, and an author may have written one or more books/articles, which clearly is m:n. Normalizing the BibTeXml schema in a relational database, the authors have to move to a separate table. In addition, this allows for a separation of initials, names, titles fields etc., which in turn enhances the flexibility of the output. But let’s stay on-topic.

How do we establish such a many-to-many relation with Xdoclet (in a JBoss 3.2.x environment)?

Let’s first have a look at the relevant CMR field of the BookBean.java

    /**
     * CMR Collection containing authors information
     *
     * @return the authorOReditors
     *
     * @ejb.interface-method
     *
     * @ejb.relation
     *      name="Book-Author"
     *      role-name="Book-written-by-Author"
     *      target-multiple="yes"
     *
     * @jboss.relation-mapping
     *      style="relation-table"
     *
     * @jboss.relation-table
     *      table-name="BookAuthor"
     *      create-table="true"
     *      remove-table="false"
     *
     * @jboss.relation
     *      related-pk-field="authorID"
     *      fk-column="publication_id_fk"
     *      fk-constraint="false"
     *
     */
    public abstract Collection getAuthors();

    /**
     * @param java.util.Set the new authors value
     *
     * @ejb.interface-method
     */
    public abstract void setAuthors(Collection authors);

Comments:

  • The collection contains AuthorLocal objects, AuthorLocal being generated by Xdoclet
  • The many-to-many character of our relationship is indicated by the target-multiple="yes" field of the @ejb.relation tag (for more details, see my previous post on this subject)
  • The linking table needed for the m:n relationship, is automatically generated by JBoss, if it does not exist already and is indicated by the @jboss.relation-mapping tag
  • Information on the linking table created by JBoss is contained in the @jboss.relation-table tag. The table is created if it didn’t exists already, but is not removed during un/redeployment (remove-table="false")
  • The @jboss.relation has already been discussed extensively in my previous post

On “the other side”, we have to define a matching relationship:

    /**
    * CMR Collection containing book information
    *
    * @return the book
    *
    * @ejb.interface-method
    *
    * @ejb.relation
    *      name="Book-Author"
    *      role-name="Author-writes-books"
    *      target-multiple="yes"
    *
    * @jboss.relation-table
    *      table-name="BookAuthor"
    *      create-table="true"
    *      remove-table="false"
    *
    * @jboss.relation-mapping
    *      style="relation-table"
    *
    * @jboss.relation
    *      related-pk-field="publicationIDfk"
    *      fk-column="author_id"
    *      fk-constraint="false"
    */
   public abstract Collection getBooks();

   /**
    * @param java.util.Set the new books value
    *
    * @ejb.interface-method
    */
   public abstract void setBooks(Collection books);

Comments:

  • The name of the @ejb.relation must be the same on both sides
  • Look carefully at at the way in which the @jboss.relation fields are defined

5 Comments

  1. Pingback: Credit Monitoring Services April 28, 2007
  2. Pingback: Student Credit Cards April 26, 2007
  3. Pingback: File Cabinets April 24, 2007
  4. Pingback: Furniture Guys April 22, 2007
  5. John Lim January 3, 2006
  6. Zeger Hendrikse June 2, 2005
  7. stephen smithstone June 1, 2005
  8. Zeger Hendrikse May 18, 2005
  9. Natalia Camargo May 18, 2005