Take a look at the "post-set" binding attribute
(http://jibx.sourceforge.net/tutorial/binding-extend.html):

Add this to your binding file:

<mapping name="person" class="com.package.Person" post-set="postSet">
  <value name="name" field="name" />
  <collection name="employees" add-method="addEmployee"
item-type="Person" usage="optional" />
</mapping>

And this to Person:
public void postSet( IUnmarshallingContext ctx )
{
  if ( ctx.getStackDepth( ) > 1 )
  {
    setEmployer( ( Person ) ctx.getStackObject( 1 ) );
  }
}

The trick is to get at the unmarshalling context and go up one level in
the stack to the parent (but make sure to check the stack depth as shown
above and make sure that you're dealing with a collection element!).

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Vairoj A.
Sent: Thursday, November 03, 2005 4:36 AM
To: [email protected]
Subject: [jibx-users] Mapping bi-directional association

Hi,

I would like to create a mapping for the following domain object.

class Person {
  private Person employer;
  private Set<Person> employees;

  public void addEmployee(Person employee) {
     employee.setEmployer(this);
     this.employees.add(employee);
  }
 
  public void setEmployer(Person employer) {
     this.employer = employer;
  }
}

Each person has one employer and many employees. The relationship is 
bi-directional as you can see from the code above. The bi-directional 
relationship is managed by method Person#addEmployee. You can see that 
when add an employee to a person will in turns set the employee's 
employer automatically.

The problem here is that, when using JiBX library to construct objects 
from XML file, the standard mapping approach does not use 
Person#addEmployee method, but it map class members directly. The XML 
file using is look like this:

<person>
  <name>James Clark</name>
  <employees>
     <person>
        <name>James Iha</name>
     </person>
     <person>
        <name>James Brown</name>
     </person>
  </employees>
</person>

 From the XML file, there is no element to be mapped to Person#employer 
field. Therefore, when construct objects from XML file, all employer 
reference in each Person object are gone.

So here is the question, how can I maintain the reference in 
Person#employer field when unmarshalling from XML?

Regards,

-- 
Vairoj Arunyaangkul
Infowave (Thailand) Co., Ltd.
[EMAIL PROTECTED]
Tel: +66.2.654.3550
Fax: +66.2.654.3555
http://www.waveman.com




-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server.
Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to