Re: circular references and ORM

Discussion in 'Java' started by Mark Thornton, May 7, 2008.

  1. Rex Mottram wrote:
    > I know that "premature optimization is the root of all evil" but OTOH
    > there are situations where you want to think about future needs before
    > putting hands to keyboard .... I'm building a web app which currently
    > has no persist store. There is, however, a plan to retrofit a
    > persistence framework of the JPA/Hibernate sort eventually, and I'm
    > wondering if circular object references are going to be a problem when
    > that time comes.
    >
    > In other words I've got code now where a Barrel contains a collection of
    > Monkeys, and each Monkey contains a reference back to its containing
    > Barrel, so that the Barrel class can have (say) a getMonkeyIterator()
    > method and the Monkey class can have a getBarrel().


    This is absolutely normal and works fine with JPA.

    Mark Thornton
    Mark Thornton, May 7, 2008
    #1
    1. Advertising

  2. Mark Thornton

    Daniel Pitts Guest

    Mark Thornton wrote:
    > Rex Mottram wrote:
    >> I know that "premature optimization is the root of all evil" but OTOH
    >> there are situations where you want to think about future needs before
    >> putting hands to keyboard .... I'm building a web app which currently
    >> has no persist store. There is, however, a plan to retrofit a
    >> persistence framework of the JPA/Hibernate sort eventually, and I'm
    >> wondering if circular object references are going to be a problem when
    >> that time comes.
    >>
    >> In other words I've got code now where a Barrel contains a collection
    >> of Monkeys, and each Monkey contains a reference back to its
    >> containing Barrel, so that the Barrel class can have (say) a
    >> getMonkeyIterator() method and the Monkey class can have a getBarrel().

    >
    > This is absolutely normal and works fine with JPA.
    >
    > Mark Thornton
    >

    In particular for JPA and Hibernate using annotations, you have a
    @OneToMany(targetEntity=Monkey.class) Set<Monkey> monkeys; in the
    Barrel, and a @ManyToOne Barrel barrel; in the Monkey class.

    What's important to remember in Relational theory is that ALL relations
    include two endpoints, whether you model that explicitly or not.
    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, May 8, 2008
    #2
    1. Advertising

  3. Mark Thornton

    Daniel Pitts Guest

    Lew wrote:
    > Daniel Pitts wrote:
    >> In particular for JPA and Hibernate using annotations, you have a
    >> @OneToMany(targetEntity=Monkey.class) Set<Monkey> monkeys; in the
    >> Barrel, and a @ManyToOne Barrel barrel; in the Monkey class.
    >>
    >> What's important to remember in Relational theory is that ALL
    >> relations include two endpoints, whether you model that explicitly or
    >> not.

    >
    > I have trouble remembering who "owns" the relation, so that the other
    > one can say "mappedBy" in its annotation. Monkey owns the Monkey-Barrel
    > connection, right? Something like
    >
    > @OneToMany( targetEntity=Monkey.class, mappedBy="barrel" )
    > Set <Monkey> monkeys;
    > ?
    >

    You *can* do that (and often it makes sense). I believe there is also a
    way to have a third table that is just for the relation, something like
    Monkey_Barrel (id, Monkey_Id, Barrel_id). Which might make more sense
    if semantically neither monkey nor barrel should "own" the reference.
    Ofcourse, this does have performance implications :p

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, May 8, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan C Douglas

    Circular references are not supported

    Dan C Douglas, Aug 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,414
    Steve C. Orr, MCSD
    Aug 13, 2003
  2. Bill Jones

    Re: Circular References

    Bill Jones, Apr 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    428
    =?Utf-8?B?RGF2ZQ==?=
    Apr 8, 2004
  3. =?Utf-8?B?S2VpdGggRi4=?=

    Circular References in Visual Studio

    =?Utf-8?B?S2VpdGggRi4=?=, Aug 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    6,282
    Cowboy \(Gregory A. Beamer\) [MVP]
    Aug 6, 2004
  4. Kiuhnm
    Replies:
    16
    Views:
    738
    Jonathan Mcdougall
    Jan 3, 2005
  5. JR

    Quotes and circular references

    JR, Sep 8, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    147
    Brian Harnish
    Sep 9, 2003
Loading...

Share This Page