openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Patrick Linskey" <plins...@gmail.com>
Subject Re: Circular Dependencies and Bidirectional Relationships
Date Fri, 03 Aug 2007 17:58:38 GMT
Hi,

> 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?

I don't think that you can avoid circularity in bidirectional
relationships. You could probably remove Java circularity and rely on
JPQL to traverse the other direction, but that's just moving the
definitions around a bit.

> Perhaps creating a BookAuthor relationship class would be a viable option?

I think that you'd still have a cycle in there somewhere if you wanted
to be able to traverse in both directions in Java code.

> 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.

You can specify cascade-delete and foreign key cascade behavior on
unidirectional relationships, so the model should remain consistent.

-Patrick

On 8/3/07, Geoffrey Downs <downsgs@us.ibm.com> wrote:
>
>
>
>
>
> 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,
> etc.?
>
> 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.
>
> Thanks,
> -Geoff Downs


-- 
Patrick Linskey
202 669 5907

Mime
View raw message