Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: circular references and ORM

Reply
Thread Tools

Re: circular references and ORM

 
 
Mark Thornton
Guest
Posts: n/a
 
      05-07-2008
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

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      05-07-2008
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/>
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      05-08-2008
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/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Semi-circular definitions (plus circular references) Kiuhnm C++ 16 01-03-2005 03:49 AM
Circular References in Visual Studio =?Utf-8?B?S2VpdGggRi4=?= ASP .Net 3 08-06-2004 04:42 PM
Re: Circular References Bill Jones ASP .Net 1 04-08-2004 10:16 PM
Quotes and circular references JR Perl Misc 3 09-09-2003 04:26 PM
Circular references are not supported Dan C Douglas ASP .Net 1 08-13-2003 09:21 PM



Advertisments