xml binding - How to XML Bind an Apache Cayenne generated class -
first off, apologies long question.
i have number of classes generated cayenne such follows.
public abstract class _form extends cayennedataobject { public static final string name_property = "name"; public static final string form_element_property = "formelement"; public static final string form_element1_property = "formelement1"; public static final string form_element2_property = "formelement2"; public static final string id_pk_column = "id"; public void setname(string name) { writeproperty("name", name); } public string getname() { return (string)readproperty("name"); } public void addtoformelement(formelement obj) { addtomanytarget("formelement", obj, true); } public void removefromformelement(formelement obj) { removetomanytarget("formelement", obj, true); } @suppresswarnings("unchecked") public list<formelement> getformelement() { return (list<formelement>)readproperty("formelement"); } } i wish turn xml schema, preferably using bind notation. first thing note above code "_form.java" file, yet there generated "form.java" file extends class shown below. i'm aware "_form.java" (above) should not changed.
import forms.cayenne.persistent.auto._form; public class form extends _form { } in essence, wish convert this, , few other classes, xml have done in simple class example below, using xml bind.
import javax.xml.bind.annotation.xmlattribute; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement(name = "form") public class form { string name; long id; @xmlelement public string getname() { return name; } public void setname(string name) { this.name = name; } @xmlattribute public long getid() { return id; } public void setid(long formid) { this.id = formid; } } what changes should making generated classes generate following xml?
<form id="1"> <name>test</name> </form>
if need serialize xml form entity, no relationships, can use custom class generator templates. templates examples below cayenne 3.1. few customizations need:
create superclass template copying here , add jaxb annotations getters in attribute section (a loop starting '## create attribute set/get methods').
in custom sublcass template can add 'getid' method , '@xmlseealso' annotation referencing superclass. of course since allowed change subclass, can add manually, instead of customizing template. 'getid' may this:
public int getid() { return cayenne.intpkforobject(this); }
then pick custom template class generator (be maven, ant or modeler). e.g. maven use 'supertemplate' parameter.
now if need more complex serialization (like do) includes various relationships, , differs request request same object, jaxb annotations limited that. , need write context sensitive jaxb marshaller or jax-rs messagebodywriter (if using jax-rs).
Comments
Post a Comment