About Question enthuware.oce-jpad.v6.2.535 :

Moderator: admin

Post Reply
dagrawal
Posts: 3
Joined: Thu Jan 05, 2012 9:14 am
Contact:

About Question enthuware.oce-jpad.v6.2.535 :

Post by dagrawal »

Why foreignkey column will be created on both the tables?

Won't JPA assume this relationship is using a jointable?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.535 :

Post by admin »

Hello,
As the explanation says, "In this case, since there is no mappedBy attribute for either side, a foreignkey column will be created on both the tables."

So basically, it is acting like two independent one to one relationships instead of one bidirectional one to one relationship.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

heaven

Re: About Question enthuware.oce-jpad.v6.2.535 :

Post by heaven »

I believe there is no such thing as two unidirectional relationships.
The conditions for a unidirectional relationship as stated by spec:

Code: Select all

Entity A references a single instance of Entity B.
Entity B does not reference Entity A.
Since there is a reference from B to A, this is a bidirectional relationship and one side must be owning side.
So in my opinion option D is correct.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.535 :

Post by admin »

heaven wrote:I believe there is no such thing as two unidirectional relationships.
The conditions for a unidirectional relationship as stated by spec:

Code: Select all

Entity A references a single instance of Entity B.
Entity B does not reference Entity A.
Since there is a reference from B to A, this is a bidirectional relationship and one side must be owning side.
So in my opinion option D is correct.
It is very much possible to have a bidirectional relationship where there is no owning side. For example, say you are modelling PublicKey and PrivateKey classes (from cryptography) where a PublicKey has a reference to its PrivateKey and vice versa, there is no owner of the relationship. So basically, there are two unidirectional relationships here.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

heaven

Re: About Question enthuware.oce-jpad.v6.2.535 :

Post by heaven »

I don't deny the possibilities of the real world. I'm just saying that the JPA 2 spec does not recognize such a situation.

In this case if there is a reference from A -> B and at the same time B -> A, bidirectional rules of the specification apply. Those rules say that for such a 1-1 relationship one side must be "owning" side, which forces usage of mappedBy on the other side.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.535 :

Post by admin »

heaven wrote:I don't deny the possibilities of the real world. I'm just saying that the JPA 2 spec does not recognize such a situation.

In this case if there is a reference from A -> B and at the same time B -> A, bidirectional rules of the specification apply. Those rules say that for such a 1-1 relationship one side must be "owning" side, which forces usage of mappedBy on the other side.
I see your point but I am not sure if the way you are interpreting the spec is right. They way I interpret is mappedBy is a must only if you want to have a bidirectional relationship. If you don't want a bidirectional relationship, you can omit mappedBy. JPA implementations seems to support this interpretation.
But again, I do see your point and to be honest I don't have a definitive answer.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

heaven

Re: About Question enthuware.oce-jpad.v6.2.535 :

Post by heaven »

I was thinking about the possibility of having such a two unidirectional relationships (besides the fact of not being recognized by spec in my opinion).
There is one thing that came to my mind that would be against such a possibility. And that is the fact that you could end up in following situation:

Code: Select all

class A{
  @OneToOne
  B b;
}
class B{
  @OneToOne
  A a;
}
after loading instanceA from database you could end up with following possibility: instanceA.b.a != instanceA, which is probably something that you do not want in case you have your relationship marked as @OneToOne.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.535 :

Post by admin »

Heaven,
Good point. May be your are right.
I guess it is good to have this discussion here linked to the question so that the users will be aware of the issues involved.

thank you for your valuable feedback,
Paul.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.535 :

Post by __JJ__ »

admin wrote:
Tue Jan 24, 2012 10:21 am
Hello,
As the explanation says, "In this case, since there is no mappedBy attribute for either side, a foreignkey column will be created on both the tables."

So basically, it is acting like two independent one to one relationships instead of one bidirectional one to one relationship.

HTH,
Paul.
I also had expected a join table to be created, because that is what is created (at least, as far as I am aware) with two independent OneToMany/ManyToOne relationships (ie with no mappedBy element on the OneToMany side). But I have just tested it and indeed it just creates an FK on both sides; so the behaviour is different for 2xindependent one to one vs 2xindependent onetomany/manytoone.

Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests