1

First off, apologies for the long question.

I have a number of classes generated by Cayenne such as 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 to turn this into an XML schema, preferably using a bind notation. The first thing to note is the above code is a "_Form.java" file, yet there is also a generated "Form.java" file that extends this class as shown below. I'm aware that "_Form.java" (above) should not be changed.

import forms.cayenne.persistent.auto._Form;

public class Form extends _Form {

}

In essence, I wish to convert this, and a few other classes, into XML like I have done in this 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 I be making to the generated classes to generate the following XML?

<Form id="1">
    <name>test</name>
</Form>
Tom Hadkiss
  • 267
  • 1
  • 7
  • 16

1 Answers1

0

If you need to serialize to XML just the Form entity, with no relationships, you can use a custom class generator templates. Templates examples below are for Cayenne 3.1. A few customizations that you will need:

  1. Create a superclass template by copying everything from here and add JAXB annotations to the getters in the attribute section (a loop starting with '## Create attribute set/get methods').

  2. In a custom sublcass template you can add 'getId' method and '@XmlSeeAlso' annotation referencing superclass. Of course since you are allowed to change subclass, you can just add those manually, instead of customizing the template. 'getId' may look like this:

    public int getId() { return Cayenne.intPKForObject(this); }

  3. Then pick a custom template for your class generator (be it Maven, Ant or Modeler). E.g. for Maven use 'superTemplate' parameter.

Now if you need more complex serialization (like I often do) that includes various relationships, and differs from request to request even for the same object, JAXB annotations are too limited for that. And you will need to write some context sensitive JAXB marshaller or JAX-RS MessageBodyWriter (if you are using JAX-RS).

andrus_a
  • 2,528
  • 1
  • 16
  • 10