Hi Søren,

If I'm understanding this correctly, I suspect the issue is that JiBX 
does not reuse objects in arrays or collections. This is an exception to 
JiBX's general policy of reusing existing objects when unmarshalling.

So just prepopulating an array of instances for unmarshalling is not 
going to work, which is what it sounds like you're doing. I don't 
understand why you can't just use the Bar factory to construct the 
instances on demand, though. Is there some problem with this? The 
factory method can get access to the owning object(s) (see 
http://jibx.sourceforge.net/tutorial/binding-extend.html#extmeths), so 
any information you need from these is available.

  - Dennis

Dennis M. Sosnoski
SOA and Web Services in Java
Training and Consulting
http://www.sosnoski.com - http://www.sosnoski.co.nz
Seattle, WA +1-425-939-0576 - Wellington, NZ +64-4-298-6117



Søren Andersen wrote:
> Hello,
>
> I've asked a few stupid questions before but I've again run into a 
> problem I can't figure out :-)
>
> Basically, I'm marshalling, then unmarshalling, then comparing two 
> objects to see if they're the same. The objects are rather large 
> object graphs, and uses a custom comparator object to do the 
> comparison automatically.
> Anyway, my problem is as follows:
>
> I have a class, Foo.
> Foo contains an array of other objects, Bar.
> Foo is itself contained inside Boo.
>
> So,
>
> class Foo {
>  Bar[] bars;
> }
>
> class Bar {
>  //some stuff
> }
>
> class Boo {
>   List listOfFoos;
> }
>
> Now, the special thing is, that almost all of the information in the 
> Bar class can be inferred from just knowing Boo when creating Foo.
> In order to transfer the information that cannot be inferred from what 
> I know, I create (and export) some light-weight Bar objects to 
> represent the Bar[]. These objects contain only the specific 
> information I need, and nothing else.
> Because I don't just want to set the Bar[] automatically by Jibx (most 
> of it is auto-constructed), I instead define get-method and set-method 
> in my xml:
>   <mapping class="dk.Foo" factory="dk.Factory.createFoo name="Foo">
>       <collection name="Bars" get-method="getBars" 
> set-method="jibxSetBars"/>
>    </mapping>
> (getBars returns Bar[]; setBars takes Bar[] as an argument)
> But this is where it starts to become strange - I can follow the way 
> through the constructor fine using a debugger, and I can see the 
> objects being set-up properly (the information not available 
> notwithstanding), and everything is good.
> However, once I enter the set-method */the objects inside Bar[] bars 
> have changed! /*They're completely new, with new "number identifiers". 
> (I'm using IDEA debugger and each object is assigned a number).
> The bars-array itself is the same as in the constructor, though - only 
> the objects inside it have changed. What's more, the objects inside it 
> are created by Bar's factory method.
> If you look at the ID numbers you'll see something like:
> this.bars Before:
> []: 1361
>   [0]: 1329
>   [1]: 1375
> this.bars After:
> []: 1361
>   [0]: 1397
>   [1]: 1392
>
> The Bar XML looks something like this:
>
>  <mapping abstract="true" class="dk.Bar" type-name="bar">
>    <structure name="lala" field="lala"/>    
>  </mapping>
>
>    <mapping name="BarDerivativeOne"
>             class="dk.BarDerivativeOne"
>             factory="dk.Factory.createBarDerivativeOne">
>       <structure map-as="bar" name="BarElements"/>
>    </mapping>
>
>    <mapping name="BarDerivativeTwo"
>             class="dk.BarDerivativeTwo"
>             factory="dk.Factory.createBarDerivativeTwo"> <--- this is 
> the factor that creates the "wrong" elements -->
>       <structure map-as="bar" name="BarElements"/>
>    </mapping>
>
> Now, I truly do not understand what's happening. I've checked the 
> obvious stuff - I do not change the Bar-elements in bars myself at any 
> time before the set-method (though I do see calls to the 
> createBarDerivativeTwo method).
> I also fail to see why jibx feels the need to insert these elements 
> into the Bar-array? I have a set method, but the Bar[] has been 
> changed before it's even called?
> What's perhaps even stranger is that not only are the arrays before 
> and after the same, but the array passed into jibxSetBars method /*is 
> exactly the same as the one already on the Foo object! */(== returns true)
>
> That is, it seems like (to me, anyway...) that Jibx first sets the 
> array manually, and then for good measure calls the set-method as well?
>
> I must have really screwed something up, and any help would be greatly 
> appreciated.
>
> (Note - any information from the set-method was taken before anything 
> was changed, so that's not the source)
>
> Regards,
>
> Søren Andersen
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> ------------------------------------------------------------------------
>
> _______________________________________________
> jibx-users mailing list
> jibx-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jibx-users
>   

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to