-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: One to one bi-directional mappings
PostPosted: Wed Mar 17, 2010 9:35 am 
Newbie

Joined: Wed Dec 19, 2007 7:10 am
Posts: 10
Dear readers,

Perhaps you can help? I've done a number of Hibernate mappings before
now, but something has left me puzzled. If I have objects A and B, where
A has a one to one bi-directional relationship with B, I define them as
follows:

public class A {
private B b;

...
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="FK_b", nullable=false)
public B getB()
{ return b; }
}

public class B {
private A a;

@OneToOne(mappedBy="b")
public A getA()
{ return a; }
}

This works perfectly.

However, what if I want A to have multiple instances of B, i.e.:

public class A {
protected B b1, b2;

@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="FK_b1", nullable=false)
public B getB1()
{ return b1; }

@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="FK_b2", nullable=false)
public B getB2()
{ return b2; }
}

The object B still only has one parent (A), so how is this defined?

Any thoughts are most welcome.


John


Top
 Profile  
 
 Post subject: Re: One to one bi-directional mappings
PostPosted: Wed Mar 17, 2010 10:37 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi John,

this topic is very similar to
https://forum.hibernate.org/viewtopic.php?f=1&t=1002316

In my opinion it is not possible to 'share' a relations.
Each bidirectional @OneToOne has to have it's own @OneToOne counterpart,
thats what's the standard defines.


Top
 Profile  
 
 Post subject: Re: One to one bi-directional mappings
PostPosted: Wed Mar 17, 2010 11:03 am 
Newbie

Joined: Wed Dec 19, 2007 7:10 am
Posts: 10
pb00067 wrote:
Hi John,

this topic is very similar to
https://forum.hibernate.org/viewtopic.php?f=1&t=1002316

In my opinion it is not possible to 'share' a relations.
Each bidirectional @OneToOne has to have it's own @OneToOne counterpart,
thats what's the standard defines.


It is the same and it's shocking that Hibernate doesn't support such a simple relationship - it's entirel valid. There are many scenerios where the relationship is valid, as pointed out by the person in the thread, and it's very easy for Hibernate to figure out which A is owned by a B:

from A where b1=instance_of_b or b2=instance_of_b

Will this ever be implemented?

Indeed, in the case of:

public class Mother {
private Child mostIntelligent, leastIntelligent;
private Child tallest;
}

public class Child {
private Mother;
}

How would this be implemented?


Top
 Profile  
 
 Post subject: Re: One to one bi-directional mappings
PostPosted: Wed Mar 17, 2010 11:43 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
It is the same and it's shocking that Hibernate doesn't support such a simple relationship - it's entirely valid



It it effectively entirely valid ?

Code:
+-------------------------+                                                 +---------------------+
|                         |   ----tallest------------------- \              |                     |
|      MOTHER             |   ---mostintelligent-------------->-------------|       CHILD         |
|                         |   ---leastintelligent------------/              |                     |
+-------------------------+                                                 +---------------------+


I never saw in UML such weird 'merged' bidirectional relations.


Top
 Profile  
 
 Post subject: Re: One to one bi-directional mappings
PostPosted: Wed Mar 17, 2010 11:49 am 
Newbie

Joined: Wed Dec 19, 2007 7:10 am
Posts: 10
pb00067 wrote:
Quote:
It is the same and it's shocking that Hibernate doesn't support such a simple relationship - it's entirely valid



It it effectively entirely valid ?

Code:
+-------------------------+                                                 +---------------------+
|                         |   ----tallest------------------- \              |                     |
|      MOTHER             |   ---mostintelligent-------------->-------------|       CHILD         |
|                         |   ---leastintelligent------------/              |                     |
+-------------------------+                                                 +---------------------+



I never saw in UML such weird 'merged' bidirectional relations.


Perhaps UML isn't always relevant in today's world? It's clearly a valid OO model, and a common one.

I haven't met anyone who's used UML in about a decade.. :)


Top
 Profile  
 
 Post subject: Re: One to one bi-directional mappings
PostPosted: Wed Mar 17, 2010 11:57 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I think persisting such relation information 'tallest','mostintelligent' etc.
fit most in that what is called 'adding columns to join tables' (book Java Persistence with Hibernate chapter 7.2.3).
(In UML these are modeled as association classes).


Top
 Profile  
 
 Post subject: Re: One to one bi-directional mappings
PostPosted: Fri Mar 19, 2010 4:41 am 
Newbie

Joined: Wed Dec 19, 2007 7:10 am
Posts: 10
pb00067 wrote:
I think persisting such relation information 'tallest','mostintelligent' etc.
fit most in that what is called 'adding columns to join tables' (book Java Persistence with Hibernate chapter 7.2.3).
(In UML these are modeled as association classes).


I've thought of another example: An Article and a Person object. An Article can have an author and editor, both of which are instances of Person.

So, if Hibernate can do this then that's great, but it should be supported through a simple one to one mapping without join tables. Anyway, time to read the join table docs.

Although, it would be great if someone could provide some example annotations to demonstrate how this problem is solved?


Top
 Profile  
 
 Post subject: Re: One to one bi-directional mappings
PostPosted: Fri Mar 19, 2010 6:09 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
I've thought of another example: An Article and a Person object. An Article can have an author and editor, both of which are instances of Person.


If you really want that a determinate person can be the author (and editor) of maximal one article,
then I would map it in following way:

Code:
@Entity
public class Article {
    @Id
    ...

    @OneToOne
    protected Person author;

   @OneToOne
    protected Person editor;
   
}

@Entity
public class Person {
   @Id
    ...

   @OneToOne(mappedBy="author")
   protected Article authorOf;

  @OneToOne(mappedBy="editor")
   protected Article edited;
}


In this way there's no join table and the 2 relationships are clearly distinguished.
Also the schema is easy readable and intuitive.

If you indeed you don't want to declare a separate relation for each type (tallest, mostintelligent, leastIntelligent, etc.),
then I think that a join table (cross-table) becomes indispensable. I can't imagine how it should work without.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.