0

I'm trying to add an extra column, using binding file to a model obtained from a xsd (a big one). The added field need to be persisted, but not serialized.

I tried with hj:generated-property but it does nothing with it.

To give a sample of what I tried so far, I tested using PO Sample from git sources on tag 0.6.0 ejb/tests/po-customized and I added this to bindings...

bindings.xjb

...
        <jaxb:bindings node="xs:complexType[@name='PurchaseOrderType']">
            <hj:entity>
                <orm:table name="po"/>
                <!-- adding creation timeStamp -->
                <hj:generated-property name="creationTimestamp" propertyName="creationTimestamp" propertyQName="creationTimestamp"
                    propertyKind="xs:dateTime" />
            </hj:entity>
        </jaxb:bindings>
...

When running mvn clean test, PurchaseOrderType doesn't have the new field. Tests run with no errors.

Is it possible to add a field like this?

2 Answers2

0

Not possible. hj:generated-property is used to customize generate properties, not to generate new properties.

Consider using something like Code Injector plugin or maybe specifying a superclass for your generated classes. The superclass would have additional fields.

Disclosure: I'm the author of Hyperjaxb3.

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221
0

Implementing Code Injector solution to resolve as suggested by @lexicore (Thanks!)

I needed to make changes in two files as follow:

bindings.xjb

...
        <jaxb:bindings node="xs:complexType[@name='PurchaseOrderType']">
            <hj:entity>
                <orm:table name="po"/>
                <!-- REMOVED! hj:generated-property -->
            </hj:entity>
            <ci:code>
// Added for DB only, avoid XML serialization
@XmlTransient
protected Calendar creationTimestamp;

@Basic
@Column(name = "CREATION_TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
public Calendar getCreationTimestamp() { return this.creationTimestamp; }

public void setCreationTimestamp(Calendar creationTimestamp) { this.creationTimestamp = creationTimestamp; }
            </ci:code>
        </jaxb:bindings>
...

pom.xml

Add arg in maven-jaxb21-plugin config

...
<arg>-Xinject-code</arg>
...

Take into account, in added code, there are some classes that reference to imported packages, if they are not imported, you need to put fully qualified name in injected code. It is not possible to add import as injected code.

Community
  • 1
  • 1