【问题标题】:hyperjaxb3 add extra columns (ie creation timestamp)hyperjaxb3 添加额外的列(即创建时间戳)
【发布时间】:2015-03-25 10:04:08
【问题描述】:

我正在尝试添加一个额外的列,使用绑定文件到从 xsd(一个大的)获得的模型。添加的字段需要持久化,但不需要序列化。

我尝试使用hj:generated-property,但它没有任何作用。

为了提供迄今为止我尝试过的示例,我在标签 0.6.0 ejb/tests/po-customized 上使用 PO Sample from git sources 进行了测试,并将其添加到绑定中...

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>
...

运行mvn clean test 时,PurchaseOrderType 没有新字段。测试运行没有错误。

是否可以添加这样的字段?

【问题讨论】:

    标签: java hibernate xsd jaxb hyperjaxb


    【解决方案1】:

    不可能。 hj:generated-property用于自定义生成属性,不是生成新属性。

    考虑使用类似Code Injector 的插件,或者为生成的类指定一个超类。超类会有额外的字段。

    披露:我是 Hyperjaxb3 的作者。

    【讨论】:

    • 完美!...添加以下分辨率以供将来参考
    【解决方案2】:

    按照@lexicore 的建议实施Code Injector solution 来解决(谢谢!)

    我需要对两个文件进行如下更改:

    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

    ma​​ven-jaxb21-plugin 配置中添加 arg

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

    考虑到,在添加的代码中,有一些类引用了导入的包,如果没有导入,则需要在注入的代码中加上全限定名。无法将import 添加为注入代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      相关资源
      最近更新 更多