The reason you need to provide a test-method to make the collection optional when you don't use a field is that there's no other way for JiBX to tell whether the collection is present. If you use the field form of collection JiBX recognizes a null value for the field as meaning the collection is not present.

 - Dennis

Vairoj A. wrote:

I think it is ok to just add iter-method in order for the class to support 2-ways binding. I added iter-method to the class and now the binding file compiling correctly.

However, in order to use usage="optional" attribute, I have to also provide test-method. I am wondering what is the default test-method for Collection classes? As when using field mapping to HashSet, there is no need to specified test-method.

So the final mapping is looks like this:

<collection item-type="com.waveman.poc.Person" add-method="addEmployee" iter-method="getEmployeesIterator" />

Thank you for your suggestion. I am better understand on the binding mechanism. Now I have one other question regarding class inheritance which I will post in another email.

Vairoj


--__--__--

Message: 2
Date: Wed, 09 Nov 2005 01:52:03 -0800
From: Dennis Sosnoski <[EMAIL PROTECTED]>
To: [email protected]
Subject: Re: [jibx-users] Re: Mapping bi-directional association
Reply-To: [email protected]

Ah, my mistake - you can use the add-method alone if you specify the binding direction="input", just drop the field="employees" attribute from the <collection>. I don't think there's currently any way to do this for a binding that supports both input and output without adding a method to the Person class (such as an iter-method that returns an iterator for the items from the set).

The basic issue is that, as you said, the add-method is in the Person class, not the Set<Person> class. If you don't specify field="employees" on the <collection> element JiBX will look for the method on the containing class, in this case Person.

  - Dennis

Vairoj A. wrote:

Hi Dennis,

Thank you for the reply. It would be very great if all I need to do is define the add-method attribute. According to my reply to Joshua, I have tried adding add-method attribute to the collection mapping but that does not work. Maybe I still does not understand your intention correctly.

What I was understand from Joshua suggestion is to add 'add-method' attribute to the collection mapping. So the mapping might looks like this:

<mapping name="person" class="com.package.Person">
  <value name="name" field="name" />
<collection name="employees" field="employees" add-method="addEmployee" type="java.util.HashSet" item-type="Person" />
</mapping>

This mapping results in binding compilation error, as there is no "addEmployee" method defined in HashSet. As the class looks like this:

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

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

  public void setEmployer(Person employer) {
     this.employer = employer;
  }
}

As I understand the add-method attribute is to define the add-method for the collection. However, the custom add-method used here is in Person class.

Is it possible to tell mapping to use custom add method that does not reside in collection class?

Cheers,
Vairoj






-------------------------------------------------------
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