Hi Sundeep,
You've got a couple of separate issues here, I think. First off is the
NPE. You say that "the customer object is being loaded through lazy
loading while loading the order object." That's probably the cause of
your NPE, since you have JiBX using the customer filed directly in the
binding. If you change your binding to use the access method for the
customer information in the order, rather than the field, it'll
hopefully fix the NPE (since calling the access method should tell
Hibernate to load the customer).
Second is the actual structure of your binding. There should be no
problem in creating the XML you give below from your classes. Given what
you've told us, you don't need to include a <collection> element as part
of the binding for Customer class at all - you instead just want the
customer information that applies to the Order, rather than the complete
list of orders for a customer. In fact, the simplest way to do this is
to just fill in the details of the Customer information you want within
the <orderTb> element as part of the OrderTb mapping:
<mapping class="com.redbus.homedelivery.orm.OrderTb" name="orderTb">
<value style="element" name="order-id" field="orderId"/>
<structure get-method="getCustomer" set-method="setCustomer">
<value style="element" name="customer-id" field="customerId"/>
<value style="element" name="customer-name" field="customerName"/>
</structure>
</mapping>
- 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
Sundeep Malali wrote:
> The point is that I can have only objects namely order and customer.
> The order object is being loaded through hibernate and the customer
> object is being loaded through lazy loading while loading the order
> object. So I will only have the order object with a reference to the
> customer object and this is what I can pass to the marshaller.
>
> Is there not any way to create the xml of the parent while creating
> the child xml ? that will be the ideal solution to my case.
>
> I want the xml equivalent of something like (other details not show in
> the xml below) -
>
> <orderTb>
> <customer>
> <order.getCustomer().getCustomerId()/>
> <order.getCustomer().getCustomerName()/>
> </customer>
> </orderTb>
>
>
> Regds
> Sundeep
>
>
> On Tue, Aug 26, 2008 at 7:16 PM, karthik govindaswamy
> <[EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>> wrote:
>
>
>
>
> ie. one customer can have multiple order is what you are
> looking at then the structure should be something like
> this...when you have multiple records...
>
> <orderTb>
> <customer>
>
> <customer-id>1</customer-id>
> <customer-name>xxxxxx</customer-name>
> <order-id>ORD123</order-id>
> <order-id>ORD1234</order-id>
> <customer>
> <customer>
> <customer-id>2</customer-id>
> <customer-name>yyyyyy</customer-name>
> <order-id>ORD111</order-id>
> <order-id>ORD1234</order-id>
> <customer>
> </orderTb>
> Does this structure solve your logic. In that case you wil have
> three class
> orderTB
> Customer
> Order-Id
>
> class orderTB{
> ArrayList CustomerList;
> Add get and set method.
> }
> Class Customer{
> String customer-id;
> String CustomerName;
> ArrayList Order-IdList;
> Add get and set method.
> }
> Class order-Id{
> String order-idValue;
> Add get and set method.
> }
>
> The binding xml will be.
>
> <
>
> binding value-style="attribute">
> <mapping class="orderTB" name="orderTB" >
> <collection field="CustomerList" usage="optional">
> <structure name="Customer" map-as="Customer"
> usage="optional"/>
> </collection>
> </mapping>
>
> <
>
> mapping type-name="Customer" class="Customer" abstract="true">
>
> <
>
> value style="element" name="customer-id" field="customer-id"
> usage="optional"/>
>
> <value style="element" name="CustomerName" field="customer-id"
> usage="optional"/>
>
> <collection field="Order-IdList" usage="optional">
> <structure name="Order-Id" map-as="Order-Id"
> usage="optional"/>
> </collection>
> </mapping>
>
>
>
> <mapping type-name="Order-Id" class="Order-Id" abstract="true">
>
> <value style="element" name="Order-Id" field="order-idValue"
> usage="optional"/>
>
> </mapping>
>
> </binding>
>
> and your main class for marshalling would not change. Let me know
> how it goes.
>
>
> Thanks
>
> Karthik
>
>
> The point is, there will be only one customer tag associated to
> order since customer is the parent and has a one-to-many relation
> with order.
>
> Regds
> Sundeep
>
>
> On Tue, Aug 26, 2008 at 6:48 PM, karthik govindaswamy
> <[EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>> wrote:
>
> you havent said how the multiple mapping is going to be..
> What would be the xml strucuture look like if you have
> multiple customers and orders?? i guess this is the
> structure you want...
>
> <orderTb>
> <order-id ordernum = "ORD123>
> <customer>
> <customer-id>1</customer-id>
> <customername>xxxxxx</customername>
> <customer>
> <customer>
> <customer-id>2</customer-id>
> <customername>xxxxxx</customername>
> <customer>
> </order-id>
> </orderTb>
>
> or
>
> <orderTb>
> <customer>
> <order-id>ORD123</order-id>
> <customer-id>1</customer-id>
> <customername>xxxxxx</customername>
> <customer>
> <customer>
>
> <order-id>ORD123</order-id>
> <customer-id>2</customer-id>
> <customername>xxxxxx</customername>
> <customer>
> </orderTb>
>
> correct me if i misunderstood your reqt..
>
> On Tue, Aug 26, 2008 at 9:02 AM, Sundeep Malali
> <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>
> wrote:
>
> Something like this -
>
> <orderTb>
> <order-id>ORD123</order-id>
> <customer>
> <customer-id>1</customer-id>
> <customername>xxxxxx</customername>
> <customer>
> </orderTb>
>
> Regds
> Sundeep
>
>
> On Tue, Aug 26, 2008 at 6:06 PM, karthik govindaswamy
> <[EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>> wrote:
>
> what is the resultant xml structure that you are
> trying to achieve,,,!!!
>
> On Tue, Aug 26, 2008 at 8:16 AM, Sundeep Malali
> <[EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>> wrote:
>
> Hi
>
> I have just started using JIBX and have a query.
>
> I have 2 classes which share a parent-child
> one-to-many relation. For displaying purposes,
> I get a list of all children (in general, not
> specific to a parent) and also the parent info
> for each child. Now I pass each child object
> to convert to xml. But while converting the
> child object, I also want the parent to be
> converted to xml.
>
> The binding info is given below :
>
> _*Child object binding:*_
> <mapping
> class="com.redbus.homedelivery.orm.OrderTb"
> name="orderTb">
> <value style="element" name="order-id"
> field="orderId"/>
> <structure field="customer"/> - This is
> the parent object
> </mapping>
>
> _*Parent object binding*_:
> <mapping
> class="com.redbus.homedelivery.orm.Customer"
> name="customer">
> <value style="element" name="customer-id"
> field="customerId"/>
> <collection field="orders"
> item-type="com.redbus.homedelivery.orm.OrderTb"/>
> - This is the child object.
> </mapping>
>
> While using this binding, I get a null pointer
> exception in the parent object.
>
> ----------------------------------------------------------------------------------------------------------------
> Caused by: java.lang.NullPointerException
> at
>
> com.redbus.homedelivery.orm.Customer.JiBX_HomeDelivery_jibx_binding_binding_marshal_1_0(Customer.java)
> at
>
> com.redbus.homedelivery.orm.JiBX_HomeDelivery_jibx_binding_bindingCustomer_access.marshal()
> at
>
> com.redbus.homedelivery.orm.OrderTb.JiBX_HomeDelivery_jibx_binding_binding_marshal_1_0(OrderTb.java)
> at
>
> com.redbus.homedelivery.orm.JiBX_HomeDelivery_jibx_binding_bindingOrderTb_access.marshal()
> at
>
> com.redbus.homedelivery.orm.OrderTb.marshal(OrderTb.java)
> at
>
> org.jibx.runtime.impl.MarshallingContext.marshalRoot(MarshallingContext.java:1041)
> at
>
> org.jibx.runtime.impl.MarshallingContext.marshalDocument(MarshallingContext.java:1111)
> at
>
> com.redbus.homedelivery.common.XMLUtils.getXML(XMLUtils.java:27)
>
> ----------------------------------------------------------------------------------------------------------------
>
> The code to perform the conversion is given
> below :
>
> public static String getXML(Object obj) {
> String xml = "";
> try {
> IBindingFactory bfact;
> bfact =
> BindingDirectory.getFactory(obj.getClass());
> // marshal root object back out to
> document in memory
> IMarshallingContext mctx =
> bfact.createMarshallingContext();
> mctx.setIndent(2);
> ByteArrayOutputStream bos = new
> ByteArrayOutputStream();
> mctx.marshalDocument(obj, "UTF-8",
> null, bos);
> xml = new
> String(bos.toByteArray(),"UTF-8");
> } catch (Exception e) {
> throw new
> MarshallingException("Unable to marshal from
> the object " + obj, e);
> }
> return xml;
> }
>
> I pass the child object to this method and
> want the parent to be also included in the
> final xml. The child object has the parent
> reference loaded properly.
>
> Any help is greatly appreciated.
>
> Regds
> Sundeep
>
>
>
> -------------------------------------------------------------------------
> 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=/
>
> <http://moblin-contest.org/redirect.php?banner_id=100&url=/>
> _______________________________________________
> jibx-users mailing list
> [email protected]
> <mailto:[email protected]>
>
> 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=/
>
> <http://moblin-contest.org/redirect.php?banner_id=100&url=/>
> _______________________________________________
> jibx-users mailing list
> [email protected]
> <mailto:[email protected]>
> 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=/
> <http://moblin-contest.org/redirect.php?banner_id=100&url=/>
> _______________________________________________
> jibx-users mailing list
> [email protected]
> <mailto:[email protected]>
> 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
> [email protected]
> 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
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users