Hi!
I'm trying to marshall hibernate persisted java object graph with JiBX,
but I have found that lazy many-to-one fields won't be marshalled, or in
some circumstances it generates exceptions.
I made a very small example where I only have two tables (PARTNER for
store people 'names', and PARTNER_RELATION to store a named relation of
two people).
My hbm.xml files are at the end of the mail, as the binding.xml too.
If I change the fetch attribute to "join" (or the lazy to
"false") in the many-to-one fields then the marshalled document
will contains the many to one structures too. Eg.:
<?xml version="1.0" encoding="UTF-8"?>
<partner>
<id>11</id>
<nev>John Smith</nev>
<leftRelations>
<partner-relation>
<id>22</id>
<relationType>friend</relationType>
<leftRelation>
<id>11</id>
<nev>John
Smith</nev>
</leftRelation>
<rightRelation> <!-- NOW IT IS NOT
LAZY -->
<id>10</id>
<nev>Andrew
Jackson</nev>
</rightRelation>
</partner-relation>
</leftRelations>
</partner>
But if the fields are lazy, then the other end of the relation will be
an empty tag. Eg.:
<?xml version="1.0" encoding="UTF-8"?>
<partner>
<id>11</id>
<nev>John Smith</nev>
<leftRelations>
<partner-relation>
<id>22</id>
<relationType>friend</relationType>
<leftRelation>
<id>11</id>
<nev>John
Smith</nev>
</leftRelation>
<rightRelation/> <!-- EMPTY
AND IT IS LAZY -->
</partner-relation>
</leftRelations>
</partner>
The lazy fields won't be marshalled even if I try to use
"get-method"/"set-method" attributes in the
binding.xml (see commented line in binding.xml) in place of
"field" attribute. But the getter method of the lazy fields will
be called in both case, because I write into the log in the method! But
there is no output for this fields.
Even if I load this lazy many-to-one fields indirectly with hibernate (I
walk the object graph from java) before marshalling, JiBX won't write
them out!
If I try to remove the usage="optional" attribute from the
fields of the abstract Partner mapping, then I got exceptions.
If the "id" is not optional then I got this:
java.lang.NullPointerException
at
hu.freesoft.xmldemo.Partner.JiBX_xmldemo_binding_marshal_1_7(Partner.java)
at
hu.freesoft.xmldemo.PartnerRelation.JiBX_xmldemo_binding_marshal_1_2(PartnerRelation.java)
at
hu.freesoft.xmldemo.JiBX_xmldemo_bindingPartnerRelation_access.marshal()
at
hu.freesoft.xmldemo.JiBX_MungeAdapter.JiBX_xmldemo_binding_marshal_1_1()
at
hu.freesoft.xmldemo.Partner.JiBX_xmldemo_binding_marshal_1_6(Partner.java)
at
hu.freesoft.xmldemo.JiBX_xmldemo_bindingPartner_access.marshal()
at
hu.freesoft.xmldemo.Partner.marshal(Partner.java)
at
org.jibx.runtime.impl.MarshallingContext.marshalRoot(UnknownSource)
at
org.jibx.runtime.impl.MarshallingContext.marshalDocument(Unknown
Source)
If only the "name" is not optional, then I got this:
org.jibx.runtime.JiBXException: null value for element
"name" from object of type
xmldemo.Partner$$EnhancerByCGLIB$$a04007f4
at
org.jibx.runtime.impl.MarshallingContext.element(Unknown Source)
at
xmldemo.Partner.JiBX_xmldemo_binding_marshal_1_5(Partner.java)
at
xmldemo.PartnerRelation.JiBX_xmldemo_binding_marshal_1_1(PartnerRelation.java)
at
xmldemo.JiBX_xmldemo_bindingPartnerRelation_access.marshal()
at
xmldemo.JiBX_MungeAdapter.JiBX_xmldemo_binding_marshal_1_1()
at
xmldemo.Partner.JiBX_xmldemo_binding_marshal_1_3(Partner.java)
at
xmldemo.JiBX_xmldemo_bindingPartner_access.marshal()
at xmldemo.Partner.marshal(Partner.java)
at
org.jibx.runtime.impl.MarshallingContext.marshalRoot(UnknownSource)
at
org.jibx.runtime.impl.MarshallingContext.marshalDocument(Unknown
Source)
Have someone meet with similar problems? Is there any solution for it?
Cheers,
Kiss Zoltán
---------------------------------------
The binding.xml file is:
<binding forwards="false"
value-style="element">
<mapping
class="xmldemo.Partner" name="partner">
<structure
map-as="partner-type"/>
<collection
name="leftRelations" field="leftRelations"
item-type="xmldemo.PartnerRelation"
factory="xmldemo.Partner.setFactory" />
<collection
name="rightRelations" field="rightRelations"
item-type="xmldemo.PartnerRelation"
factory="xmldemo.Partner.setFactory" />
</mapping>
<mapping
class="xmldemo.Partner" abstract="true"
type-name="partner-type">
<value name="id"
field="id" usage="optional"/>
<value name="name"
field="nev" usage="optional" />
</mapping>
<mapping
class="xmldemo.PartnerRelation"
name="partner-relation">
<value name="id"
field="id"/>
<value
name="relationType" field="relationType" />
<!-- get-method / set-method
even not working for lazy field -->
<!--structure
get-method="getLeftPartner"
set-method="setLeftPartner" name="leftRelation"
map-as="partner-type" /-->
<structure
field="leftPartner" name="leftRelation"
map-as="partner-type" />
<structure
field="rightPartner" name="rightRelation"
map-as="partner-type" />
</mapping>
</binding>
The hbm.xmls:
<hibernate-mapping
default-cascade="none">
<class
name="xmldemo.Partner" table="PARTNER"
dynamic-insert="false" dynamic-update="false">
<id
name="id" type="java.lang.Long"
unsaved-value="null">
<column name="ID" sql-type="NUMBER(19)"/>
<generator class="native"></generator>
</id>
<property name="name"
type="java.lang.String">
<column name="NAME" not-null="true"
sql-type="VARCHAR2(128)"/>
</property>
<set
name="leftRelations" inverse="true">
<key><column name="LEFT_PARTNER_FK"
sql-type="NUMBER(19)" not-null="true"
/></key>
<one-to-many class="xmldemo.PartnerRelation" />
</set>
<set
name="rightRelations" inverse="true">
<key><column name="RIGHT_PARTNER_FK"
sql-type="NUMBER(19)" not-null="true"
/></key>
<one-to-many class="xmldemo.PartnerRelation" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping
default-cascade="none">
<class
name="xmldemo.PartnerRelation"
table="PARTNER_RELATION" dynamic-insert="false"
dynamic-update="false">
<id
name="id" type="java.lang.Long"
unsaved-value="null">
<column name="ID" sql-type="NUMBER(19)"/>
<generator class="native"></generator>
</id>
<property name="relationType"
type="java.lang.String">
<column name="RELATION_TYPE" not-null="true"
unique="false" sql-type="VARCHAR2(128)"/>
</property>
<many-to-one name="leftPartner"
class="hu.freesoft.xmldemo.Partner" lazy="proxy"
fetch="select">
<column name="LEFT_PARTNER_FK" not-null="true"
sql-type="NUMBER(19)"/>
</many-to-one>
<many-to-one name="rightPartner"
class="hu.freesoft.xmldemo.Partner" lazy="proxy"
fetch="select">
<column name="RIGHT_PARTNER_FK" not-null="true"
sql-type="NUMBER(19)"/>
</many-to-one>
</class>
</hibernate-mapping>
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users