openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pinaki Poddar <ppod...@apache.org>
Subject How should behave with inconsistent one-to-one, bidirectional relation?
Date Thu, 22 May 2008 12:56:22 GMT

Hi,
  One-to-one, bidirectional relation between Entity A (owner) and Entity B
(owned) is mapped in JPA using a *single* foreign key in Table A to Table B
(see Section 2.1.8.1 of JPA Spec V1.0).

However, in Java, the relation is expressed in terms of two variables: e.g.
in a Master-Pet relation as Master::pet and Pet::master. Hence it leaves
room for inconsistency.

When the Java instance model is consistent i.e. for a pair of master-pet
(m1, p1) :
   m1.pet = p1 and p1.master = m1
everything works perfectly.

But if the Java instance model is inconsistent i.e. for two masters m1, m2
and two pets p1, p2
      m1.pet = p1 but p1.master = m2 (instead of m1)
and m2.pet = p2 but p2.master = m1 (instead of m2)

then there is a dilemma. 

Currently, such inconsistent relation will be 'automagically' persisted as a
consistent relation in the database. But that also means that if a Java
instance model M is store in one transaction and retrieved in another as M'
then M and M" is no more equal by value. 

a) Does this behavior violate the core premise of 'non-obtrusive',
'transparent' persistence? 
b) Should JPA Spec require to clarify further on how inconsistent relation
be handled? 


PS: I am aware that OpenJPA can enforce integrity on Java object model via
InverseManager, but this issue is related to pure JPA behavior.
 


If bidirectional relation is set consistently by the user then the 
For example, if Master and Pet are related bi-directionally with Master as
the owner of the relation, a consistent pair of Master-Pet may look like the
following in the database:
> SELECT * FROM MASTER;
-----------------------------------------------------
  ID             NAME       PET_ID
-----------------------------------------------------
   1            m1             101
   2            m2             102
-----------------------------------------------------

> SELECT * FROM PET;
----------------------------------------------------
  ID           NAME       
----------------------------------------------------
  101          p1
  102          p2
----------------------------------------------------
-- 
View this message in context: http://www.nabble.com/How-should-behave-with-inconsistent-one-to-one%2C-bidirectional-relation--tp17403683p17403683.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Mime
View raw message