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);
- The collection contains
AuthorLocalbeing generated by Xdoclet
- The many-to-many character of our relationship is indicated by the
target-multiple="yes"field of the
@ejb.relationtag (for more details, see my previous post on this subject)
- The linking table needed for the
m:nrelationship, is automatically generated by JBoss, if it does not exist already and is indicated by the
- Information on the linking table created by JBoss is contained in the
@jboss.relation-tabletag. The table is created if it didn’t exists already, but is not removed during un/redeployment (
@jboss.relationhas 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);
@ejb.relationmust be the same on both sides
- Look carefully at at the way in which the
@jboss.relationfields are defined