最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

problem with maven generation : HyperJaxb fails on Java JPA float array mapping - Stack Overflow

programmeradmin0浏览0评论

i have 2D double array attribute but as JPA doesn't work with array i have a @XmlJavaTypeAdapter(DoubleArrayAdapter.class) to store my array in a string for postgresql...

I am working with JDK 21 and Jakarta.

i use these maven plugin to generate Java classes & sql from xsd files.

org.hibernate.orm => hibernate-jpamodelgen 6.6.2.Final              
org.patrodyne.jvnet => hisrc-hyperjaxb-maven-plugin 2.2.1

my XSD for the array attribute is :

    <xsd:element name="speeds" type="xsd:string">
        <xsd:annotation>
            <xsd:appinfo>
                <xjc:javaType  name="double[][]" adapter="org.sailquest.model.adapter.DoubleArrayAdapter"/>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:element>

There is no XJB file.

The Java code generated is :

    @XmlElement(required = true, type = String.class)
    @XmlJavaTypeAdapter(DoubleArrayAdapter.class)
    @XmlSchemaType(name = "double")
    protected double[][] speeds;

    /**
     * Obtient la valeur de la propriété speeds.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    @Transient
    public double[][] getSpeeds() {
        return speeds;
    }
    
    /**
     * Définit la valeur de la propriété speeds.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setSpeeds(double[][] value) {
        this.speeds = value;
    }
    
    @Transient
    public boolean isSetSpeeds() {
        return (this.speeds!= null);
    }

The Java generation seems ok (the getter is transient ???) but there is an error message during generation and the column is not created during database sql generation.

The speeds column is not generated because there's an error during generation : [INFO] XJC> Xhyperjaxb-jpa: : Start Parameters NaiveInheritanceStrategy.: false MaxIdentifierLength......: null PersistenceUnitName......: null PersistenceXml...........: null Result...................: annotations RoundtripTestClassName...: null ValidateXml.: null (ignored) TargetDir................: null Verbose..................: true Debug....................: false [ERROR] XJC> Xhyperjaxb-jpa: TODO [ ...ataModel/Sail.xsd{30,49} ], getAttributeMapping: class=Sail, field=speeds; could not be annotated. It will be made transient.

And the getter is transient...

Why i have this message?

Thanks for your help!

Guillaume

i have 2D double array attribute but as JPA doesn't work with array i have a @XmlJavaTypeAdapter(DoubleArrayAdapter.class) to store my array in a string for postgresql...

I am working with JDK 21 and Jakarta.

i use these maven plugin to generate Java classes & sql from xsd files.

org.hibernate.orm => hibernate-jpamodelgen 6.6.2.Final              
org.patrodyne.jvnet => hisrc-hyperjaxb-maven-plugin 2.2.1

my XSD for the array attribute is :

    <xsd:element name="speeds" type="xsd:string">
        <xsd:annotation>
            <xsd:appinfo>
                <xjc:javaType  name="double[][]" adapter="org.sailquest.model.adapter.DoubleArrayAdapter"/>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:element>

There is no XJB file.

The Java code generated is :

    @XmlElement(required = true, type = String.class)
    @XmlJavaTypeAdapter(DoubleArrayAdapter.class)
    @XmlSchemaType(name = "double")
    protected double[][] speeds;

    /**
     * Obtient la valeur de la propriété speeds.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    @Transient
    public double[][] getSpeeds() {
        return speeds;
    }
    
    /**
     * Définit la valeur de la propriété speeds.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setSpeeds(double[][] value) {
        this.speeds = value;
    }
    
    @Transient
    public boolean isSetSpeeds() {
        return (this.speeds!= null);
    }

The Java generation seems ok (the getter is transient ???) but there is an error message during generation and the column is not created during database sql generation.

The speeds column is not generated because there's an error during generation : [INFO] XJC> Xhyperjaxb-jpa: : Start Parameters NaiveInheritanceStrategy.: false MaxIdentifierLength......: null PersistenceUnitName......: null PersistenceXml...........: null Result...................: annotations RoundtripTestClassName...: null ValidateXml.: null (ignored) TargetDir................: null Verbose..................: true Debug....................: false [ERROR] XJC> Xhyperjaxb-jpa: TODO [ ...ataModel/Sail.xsd{30,49} ], getAttributeMapping: class=Sail, field=speeds; could not be annotated. It will be made transient.

And the getter is transient...

Why i have this message?

Thanks for your help!

Guillaume

Share Improve this question asked Feb 6 at 13:41 ghelleghelle 11 bronze badge New contributor ghelle is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

This unit test example, named floating, from the HiSrc HyperJAXB project demonstrates how to use the hisrc-hyperjaxb-maven-plugin and -Xinject-code to generate a JAXB/JPA annotated class with a property to manage a 2D double array.

Injected Code

@jakarta.persistence.Transient
public double[][] getDoubleArray()
{
    return DoubleArrayAdapter.unmarshal(getDoubleArrayB64());
}

@jakarta.persistence.Transient
public void setDoubleArray(double[][] doubleArray)
{
    setDoubleArrayB64(DoubleArrayAdapter.marshal(doubleArray));
}

The injected code, from bindings.xjb uses this custom DoubleArrayAdapter to unmarshal/marshal the byte[] array to/from a double[][] instance. The code above is declared to be JPA transient and it does not have a JAXB annotated field; instead, it delegates binding and persistence to the doubleArrayB64 field and associated property methods.

The unit test includes these resources: schema.xsd and bindings.xjb.

The schema.xsd defines this complexType.

<xs:complexType name="floatingTypesType">
    <xs:sequence>
        <xs:element name="float"  type="xs:float"  minOccurs="0"/>
        <xs:element name="double" type="xs:double" minOccurs="0"/>
        <xs:element name="doubleArrayB64" type="xs:base64Binary"/>
    </xs:sequence>
</xs:complexType>

The complexType defines a child element, named doubleArrayB64, to bind and persist the 2D double array property. When an element's type is xs:base64Binary, the XJC generates a JAXB annotated field to manage a byte[] array and the HyperJAXB plugin generates a JPA annotated property to store the value as a @Lob, as shown below.

<xs:element name="doubleArrayB64" type="xs:base64Binary"/>

Because the XML Schema type is xs:base64Binary the contents of the byte[] contains a Base64 decoding of the XML value, in this case, a 2D double array as a delimited set of rows and columns. JAXB handles the Base64 decoding and encoding. The DoubleArrayAdapter handles the parsing of the dataset.

The hisrc-hyperjaxb-maven-plugin invokes XJC to generate the FloatingTypesType class containing these statements plus the injected code from above:

FloatingTypesType.java

...
@XmlElement(required = true)
protected byte[] doubleArrayB64;
...
@Basic
@Column(name = "DOUBLE_ARRAY_B64")
@Lob
public byte[] getDoubleArrayB64() {
    return doubleArrayB64;
}
public void setDoubleArrayB64(byte[] value)
{
    this.doubleArrayB64 = value;
}
...
(plus injected code for getDoubleArray()/setDoubleArray(...))

The example depends on these Maven artifacts.

jakarta.persistence:jakarta.persistence-api:jar:3.1.0
jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.2
org.glassfish.jaxb:jaxb-runtime:jar:4.0.5
org.hibernate.orm:hibernate-core:jar:6.4.4.Final
org.patrodyne.jvnet:hisrc-hyperjaxb-maven-plugin:jar:2.2.1
org.patrodyne.jvnet:hisrc-hyperjaxb-ejb-runtime:jar:2.2.1
org.patrodyne.jvnet:hisrc-hyperjaxb-opt-hibernate:jar:2.2.1

Disclaimer: I am the maintainer for the HiSrc projects.

发布评论

评论列表(0)

  1. 暂无评论