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
--__--__--
Message: 2
Date: Mon, 07 Nov 2005 22:47:35 -0800
From: Dennis Sosnoski <[EMAIL PROTECTED]>
To: [email protected]
Subject: Re: [jibx-users] Re: Mapping bi-directional association
Reply-To: [email protected]
Hi Vairoj,
From your original description, you should be able to just use the
add-method="addEmployee" approach originally suggested by Joshua,
without the need to use a post-set method (since your addEmployee method
takes care of setting the association link). If you're only
unmarshalling documents use the direction="input" attribute on your
<binding> element so that only the add-method is needed. If you're also
marshalling, you'll need to tell JiBX how to get the items for output; I
don't think I've tried this, but you should be able to use
field="employees" to handle the output side, while still using
add-method="addEmployee" to handle the input. You may need the CVS code
(or upcoming RC2) to make this work correctly, though.
- Dennis
Vairoj A. wrote:
Hi,
Thanks. The solution works correctly and do solve the back-reference
problem. However, the solution requires the domain class (in this
case, Person) to have direct dependency on JiBX library, which is not
allowed in our architecture. Therefore, is there any other
alternatives for this problem?
Cheers,
Vairoj
-- __--__--
Message: 2
Subject: RE: [jibx-users] Re: Mapping bi-directional association
Date: Fri, 4 Nov 2005 08:04:47 -0600
From: "Davies, Joshua" <[EMAIL PROTECTED]>
To: <[email protected]>
Reply-To: [email protected]
Whoops - guess I should have tried compiling that before I posted it,
eh? I changed the HashSet to an ArrayList and made some other
modifications to your "Person" class so I could focus on the
back-reference problem; I guess I forgot to unmake them before I copied
the solution into my e-mail.
Anyway, what I was really trying to direct you to the post-set
attribute; that will solve your problem of maintaining a "back
reference" to the person's employer, which I believe was the question in
your original post. I just ran a new compile using your original Person
class, and a (simpler) collection mapping of:
<collection name=3D"employees" field=3D"employees"
usage=3D"optional" =
/>
And got your test.xml to unmarshal correctly using the unmodified
(except for the addition of the postSet handler) Person class.
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Vairoj A.
Sent: Friday, November 04, 2005 2:13 AM
To: [email protected]
Subject: [jibx-users] Re: Mapping bi-directional association
Hi,
I have tried adding 'add-method' attribute to the binding file as you=20
suggested. However, I believe that the add-method specified here is
the=20
method for the collection. However, the addEmployee method resides
in=20
the Person class, not in the collection - in this case, HashSet
class.=20
So there is an error when running the binding compiler as follow:
Error: add-method addEmployee not found in class java.util.HashSet
for=20
collection element at (line 43, col 5, in Campaign/jibx-binding.xml)
Regards,
Vairoj
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=3D3D"person" class=3D3D"com.package.Person" =3D
post-set=3D3D"postSet">
<value name=3D3D"name" field=3D3D"name" />
<collection name=3D3D"employees" add-method=3D3D"addEmployee"
item-type=3D3D"Person" usage=3D3D"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!).
-------------------------------------------------------
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