Apache JDO Project - Managed 1-1 relationiships

This is Interesting: Free IT Magazines  
Home > Archive > Apache JDO Project > July 2005 > Managed 1-1 relationiships





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Managed 1-1 relationiships
Michael Watzek

2005-07-21, 5:45 pm

Hi Craig, Erik, Andy,

testing 1-1 relationships I encountered that the JPOX enhancer generates
code for managed relationships if the orm meta data contains attribute
"mapped-by" on a field element. Thus setting one side of the
relationship implies setting the other side, too. The decompiled
enhanced code looks like this:

public void setMentor(Employee mentor)
{
jdoSetmentor(this, mentor);
jdoSetprotege(mentor, this);
}
public void setProtege(Employee protege)
{
jdoSetprotege(this, protege);
jdoSetmentor(protege, this);
}

I'm not sure if this is spec compliant. In chapter 15.3 the spec states:

"The field on the other side of the relationship can be mapped simply by
identifying the field on the
other side that defines the mapping, using the mapped-by attribute.
Changes to the field mapped via
“mapped-by” are not reflected in the datastore. There is no further
relationship implied by having
both sides of the relationship map to the same database column(s). In
particular, making a change
to one side of the relationship does not imply any runtime behavior by
the JDO implementation to
change the other side of the relationship in memory, although the
column(s) will be changed during
commit and will therefore be visible by both sides in the next transaction."

Regards,
Michael
--
-------------------------------------------------------------------
Michael Watzek Tech@Spree Engineering GmbH
mailto:mwa.tech@spree.de Buelowstr. 66
Tel.: ++49/30/235 520 36 10783 Berlin - Germany
Fax.: ++49/30/217 520 12 http://www.spree.de/
-------------------------------------------------------------------

Andy Jefferson

2005-07-21, 5:45 pm

> testing 1-1 relationships I encountered that the JPOX enhancer generates
> code for managed relationships if the orm meta data contains attribute
> "mapped-by" on a field element. Thus setting one side of the
> relationship implies setting the other side, too. The decompiled
> enhanced code looks like this:
>
> public void setMentor(Employee mentor)
> {
> jdoSetmentor(this, mentor);
> jdoSetprotege(mentor, this);
> }
> public void setProtege(Employee protege)
> {
> jdoSetprotege(this, protege);
> jdoSetmentor(protege, this);
> }


Hi Michael,
not sure how you got it to give that but it sure doesn't for me ;-)
I'll give you a test case to demonstrate the point (and you can point out
where your case is different) ...

public class Child
{
private Parent parent;
public setParent(Parent parent)
{
this.parent = parent;
}
}
public class Parent
{
private Child child = null;
public void setChild(Child child)
{
this.child = child;
}
}

<jdo>
<package name="org.jpox.samples">
<class name="Child" detachable="true">
<field name="parent" persistence-modifier="persistent"/>
</class>

<class name="Parent" detachable="true">
<field name="child" persistence-modifier="persistent"
mapped-by="parent"/>
</class>
</package>
</jdo>

And I get the following enhanced classes mutator methods
public class Child implements PersistenceCapable, ...
{
...
public void setParent(Parent parent) {
jdoSetparent(this, parent);
}
}
public class Parent implements PersistenceCapable, ...
{
...
public void setChild(Child child) {
jdoSetchild(this, child);
}
}

Looks right to me

--
Andy

Michael Watzek

2005-07-22, 7:45 am

Hi Andy,

comments inline:

>
>
> Hi Michael,
> not sure how you got it to give that but it sure doesn't for me ;-)
> I'll give you a test case to demonstrate the point (and you can point out
> where your case is different) ...
>
> public class Child
> {
> private Parent parent;
> public setParent(Parent parent)
> {
> this.parent = parent;
> }
> }
> public class Parent
> {
> private Child child = null;
> public void setChild(Child child)
> {
> this.child = child;
> }
> }
>
> <jdo>
> <package name="org.jpox.samples">
> <class name="Child" detachable="true">
> <field name="parent" persistence-modifier="persistent"/>
> </class>
>
> <class name="Parent" detachable="true">
> <field name="child" persistence-modifier="persistent"
> mapped-by="parent"/>
> </class>
> </package>
> </jdo>

In my case, the identity type is datastore and there are separate files
for jdo metadata and orm metadata. The orm metadata file contains the
"mapped-by" attribute. I attached .java, .jdo, and .orm. I did not
attach the schema file because the attachement would become too big for
Apache. If you like to see it, let me know. Class "Employee" has fields
"mentor" and "protege". Field "protege" is
mapped by field "mentor".

I use JPOX enhancer+runtime nightly build of 14th Jul. Hope that helps.

Regards,
Michael
>
> And I get the following enhanced classes mutator methods
> public class Child implements PersistenceCapable, ...
> {
> ...
> public void setParent(Parent parent) {
> jdoSetparent(this, parent);
> }
> }
> public class Parent implements PersistenceCapable, ...
> {
> ...
> public void setChild(Child child) {
> jdoSetchild(this, child);
> }
> }
>
> Looks right to me
>



--
-------------------------------------------------------------------
Michael Watzek Tech@Spree Engineering GmbH
mailto:mwa.tech@spree.de Buelowstr. 66
Tel.: ++49/30/235 520 36 10783 Berlin - Germany
Fax.: ++49/30/217 520 12 http://www.spree.de/
-------------------------------------------------------------------

Andy Jefferson

2005-07-22, 7:45 am

> In my case, the identity type is datastore and there are separate files
> for jdo metadata and orm metadata. The orm metadata file contains the
> "mapped-by" attribute


Well I use datastore identity, and mapped-by. The fact that you're using=20
separate files is not of relevance since JPOX just merges them.

Looking at your class you have :-

public void setProtege(Employee protege) {
=A0 =A0 =A0 =A0 this.protege =3D protege;
=A0 =A0 =A0 =A0 protege.mentor =3D this;
=A0 =A0 }
public void setMentor(Employee mentor) {
=A0 =A0 =A0 =A0 this.mentor =3D mentor;
=A0 =A0 =A0 =A0 mentor.protege =3D this;
=A0 =A0 }

It is hardly surprising that it is enhanced as you say ... since you have=20
included the managed relationship line already. This is not added by the=20
enhancer. The enhancer simply corrects calls to the field to go via=20
jdoSetXXX. If the user has already added managemenet of relationships then=
=20
they remain.


=2D-=20
Andy

Michael Watzek

2005-07-22, 7:45 am

Hi Andy,

my fault - sorry. I did not realize that the unenhanced Employee class
contains code managing relationships already.

Thanks for pointing that out!

Regards,
Michael
>
>
> Well I use datastore identity, and mapped-by. The fact that you're using
> separate files is not of relevance since JPOX just merges them.
>
> Looking at your class you have :-
>
> public void setProtege(Employee protege) {
> this.protege = protege;
> protege.mentor = this;
> }
> public void setMentor(Employee mentor) {
> this.mentor = mentor;
> mentor.protege = this;
> }
>
> It is hardly surprising that it is enhanced as you say ... since you have
> included the managed relationship line already. This is not added by the
> enhancer. The enhancer simply corrects calls to the field to go via
> jdoSetXXX. If the user has already added managemenet of relationships then
> they remain.
>
>



--
-------------------------------------------------------------------
Michael Watzek Tech@Spree Engineering GmbH
mailto:mwa.tech@spree.de Buelowstr. 66
Tel.: ++49/30/235 520 36 10783 Berlin - Germany
Fax.: ++49/30/217 520 12 http://www.spree.de/
-------------------------------------------------------------------

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com