openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Geoffrey Downs <>
Subject Circular Dependencies and Bidirectional Relationships
Date Fri, 03 Aug 2007 17:21:04 GMT

Hi all,
  I have a question concerning best practices for implementing
bidirectional relationships in openJPA. I am working on a new development
project that is evaluating using openJPA as our ORM solution. Architectural
coherence is fundamentally important to the project, so we'd like to avoid
circular dependencies in our java classes whenever possible. My question
is: how do we avoid circular dependencies when dealing with bidirectional
or containment relationships in openJPA?

For example: consider the Book-Author relationship (many-to-many,
bidirectional). The corresponding Java classes might look something like:

   class Book{
      private Collection<Author> authors;
   class Author{
      private Collection<Book> books;

Unfortunately, this means that the class Book depends on the class Author,
and that the class Author also depends on the class Book. Has anyone
developed a best-practice for how to circumvent this? Perhaps creating a
BookAuthor relationship class would be a viable option? How would this work
within the context of openJPA's features like dirty checking, cascades,

Example: consider a containment relationship, such as the
Magazine-Publisher relation (Publishers have multiple magazines, every
magazine has exactly one publisher). The corresponding Java classes might
look like:

   class Magazine{
      private Publisher publisher;
   class Publisher{
      private Collection<Magazine> magazines;

Again, this introduces a circular dependency between the Magazine and
Publisher classes. We could remove this circular dependency by asserting
that the Magazine class should not know about it's parent Publisher
instance. However, this would result in a database schema that did not
reflect reality: we could easily create "orphaned" magazine objects that
had no association with a publisher object.

Any thoughts on how to resolve these issues would be creatly appreciated.

-Geoff Downs
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message