I figured out what I'd missed was using the type-name / map-as attributes to
achieve what I was after. I am now mapping the IOrderItem with a type-name
set. Then just reference that using map-as on a structure within the
collection. The name of element is defined on the structure, not on the
child object mapping. This gives the flexibility I was looking for. I knew
it had to be there, but it was a matter of getting the right binding set up!
<mapping class="IOrder" name="order" abstract="true" factory="
Order.getInstance" >
<value name="order-number" get-method="getOrderNumber"
set-method="setOrderNumber" />
<collection name="order-items" create-type="java.util.ArrayList"
get-method="getItems" set-method="setItems">
<structure name="order-item" map-as="OrderItemType" />
</collection>
</mapping>
<!-- Define IOrderItem -->
<mapping type-name="OrderItemType" class="IOrderItem" abstract="true"
factory=" OrderItem.getInstance">
<value name="item-number" get-method="getItemNumber"
set-method="setItemNumber" />
</mapping>
Thanks for your help!
Zach
>Hi Zach,
>
>I don't see offhand why the approach of using an abstract mapping for
>IOrderItem gave the ClassFormatError, though it sounds like JiBX is
>inappropriately adding code to the interface. Though now that I look at
>the binding again, I wonder if it's because you're using a
>name="order-item" on the abstract mapping. This was previously illegal
>(name on an abstract mapping), but I started allowing it to handle some
>cases that came up in the binding+schema generation from existing code -
>and that may have opened up some untested (and broken) code paths. Try
>taking off the name="order-item" and see if that works (and let us know
>the result).
>
>The second case is easier to explain. The generic handling only works
>with classes which have concrete mapping definitions, which JiBX extends
>with IMarshallable and IUnmarshallable interfaces. If you gave OrderItem
>a concrete mapping this would work - but presumably you're instead
>trying to bind the interface because you want to avoid mapping the
>implementation class(es).
>
>- 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
>
>
>
>Zachary Paul wrote:
>> I'm having an issue mapping a collection whose items are interfaces.
>> For example, I've got a Customer that has an interface IOrder as an
>> attribute. IOrder contains a List which will contain IOrderItem
>> interfaces. If I map the collection using a nested structure,
>> everything works fine.
>>
>> <mapping class="IOrder" name="order" abstract="true"
>> factory="Order.getInstance" >
>> <value name="order-number" get-method="getOrderNumber"
>> set-method="setOrderNumber" />
>> <collection name="order-items" create-type="java.util.ArrayList"
>> get-method="getItems" set-method="setItems">
>>
>> <!-- Define IOrderItem -->
>> <structure name="order-item" type="IOrderItem"
>> abstract="true" factory="OrderItem.getInstance">
>> <value name="item-number" get-method="getItemNumber"
>> set-method="setItemNumber" />
>> </structure>
>>
>> </collection>
>>
>>
>> While this works, it seems more appropriate to define IOrderItem in
>> its own mapping so that I can reuse that definition instead of
>> redefining the structure for every class that uses it. I found a post
>> in the mailing list archive that suggested something like this.
>>
>> <mapping class="IOrder" name="order" abstract="true"
>> factory="Order.getInstance" >
>> <value name="order-number" get-method="getOrderNumber"
>> set-method="setOrderNumber" />
>> <collection name="order-items" create-type="java.util.ArrayList"
>> get-method="getItems" set-method="setItems">
>> <structure name="order-item" type="IOrderItem" />
>> </collection>
>> </mapping>
>>
>> <!-- Define IOrderItem -->
>> <mapping name="order-item" class="IOrderItem"
>> abstract="true" factory=" OrderItem.getInstance">
>> <value name="item-number" get-method="getItemNumber"
>> set-method="setItemNumber" />
>> </mapping>
>>
>>
>> When I do this, I get the following error.
>>
>> The java class could not be loaded. java.lang.ClassFormatError:
>> (IOrderItem) interface method must be public and abstract at offset=265
>>
>> I've also tried defining the collection more generically...
>>
>> <mapping class="IOrder" name="order" abstract="true"
>> factory="Order.getInstance" >
>> <value name="order-number" get-method="getOrderNumber"
>> set-method="setOrderNumber" />
>> <collection name="order-items" create-type="java.util.ArrayList"
>> get-method="getItems" set-method="setItems" />
>> </mapping>
>>
>>
>> Which gives this exception...
>>
>> java.lang.ClassCastException: OrderItem incompatible with
>> org.jibx.runtime.IMarshallable
>> at JiBX_MungeAdapter.JiBX_binding_mapping_marshal_1_2()
>> at JiBX_MungeAdapter.JiBX_binding_mapping_marshal_1_3()
>> at Customer.JiBX_binding_mapping_marshal_1_0(Customer.java)
>> at JiBX_binding_mappingCustomer_access.marshal()
>> at Customer.marshal(Customer.java)
>> at org.jibx.runtime.impl.MarshallingContext.marshalRoot
>> (MarshallingContext.java:1044)
>> at
>> org.jibx.runtime.impl.MarshallingContext.marshalDocument(
MarshallingContext.java:1114)
>> at TestHarness.main(TestHarness.java:32)
>>
>>
>> The mapping does work if I use the concrete class instead of the
>> interface!
>>
>> This is the first time I've used Jibx so I'm not sure if I'm just
>> missing an option on one of the mapping elements that will make this
>> magically work or if I really just need to either use the
>> implementation class or duplicate the structure when I need to.
>>
>> Any insight would be much appreciated! I can provide the full
>> interface, implementation, and binding if it would help.
>>
>> Zach Paul
>> ------------------------------------------------------------------------
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by: Splunk Inc.
>> Still grepping through log files to find problems? Stop.
>> Now Search log events and configuration files using AJAX and a browser.
>> Download your FREE copy of Splunk now >> http://get.splunk.com/
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> jibx-users mailing list
>> [EMAIL PROTECTED]
>> https://lists.sourceforge.net/lists/listinfo/jibx-users
>>
>
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users