Page 1 of 1

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

Posted: Tue Jan 24, 2012 9:30 am
by dagrawal
Why foreignkey column will be created on both the tables?

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

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

Posted: Tue Jan 24, 2012 10:21 am
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.

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

Posted: Sun Oct 07, 2012 6:02 am
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.

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

Posted: Sun Oct 07, 2012 11:18 am
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.

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

Posted: Wed Oct 10, 2012 6:52 am
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.

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

Posted: Sun Oct 14, 2012 10:23 am
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.

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

Posted: Wed Oct 24, 2012 4:33 am
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.

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

Posted: Sun Oct 28, 2012 1:21 pm
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.

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

Posted: Sat Sep 22, 2018 4:10 pm
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.