mavenjaxbxjchyperjaxb

patrodyne/hisrc-hyperjaxb maven plugin exclude precision and scale from @Column for Double fields


My situation is close to this question. I use patrodyne/hisrc-hyperjaxb maven plugin to generate entity classes with JPA annotions from xsd schema along with krasa-jaxb-tools plugin, which add validation annotaions. There are some elements with type="xs:double" in my xsd scheme and after classes generation there are some Double fields which annotated @Column like this:

    @Column(name = "SOURCE_RATIO", precision = 20, scale = 10)
    public Double getSourceRatio() {
        return sourceRatio;
    }

Then I generate Spring Data repositories with cmeza20/spring-data-generator and use Spring Data REST dependency. Besides of all, I want to auto-create my database scheme, so I put that property in my application.properties file:

spring.jpa.hibernate.ddl-auto=create

And when I run my application I get "java.lang.IllegalArgumentException: scale has no meaning for floating point numbers". How can I configurate @Column annotation to exclude precision and scale for Double java type?

I think the plugin have no any options to configure this (maybe Mr.O'Sullivan, plugin developer, can comment on that). But maybe "binding.xjc" may help with this problem. I don't have much experience with setting up the bindings file, please help me.


Solution

  • Try adding a HyperJAXB customization for default-single-property to your XJC binding file, something like this:

    bindings.xjb

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <jaxb:bindings jaxb:version="3.0"
        xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:hj="http://jvnet.org/hyperjaxb/jpa"
        xmlns:orm="https://jakarta.ee/xml/ns/persistence/orm"
        jaxb:extensionBindingPrefixes="hj orm"
    >
        ...
        <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
    
            <hj:persistence>
                <hj:default-single-property type="xs:double">
                    <hj:basic>
                        <orm:column />
                    </hj:basic>
                </hj:default-single-property>
            </hj:persistence>
    
        </jaxb:bindings>
    </jaxb:bindings>
    

    Indeed, you identified a bug in the HypeJAXB plugin. The plugin unmarshals its own DefaultCustomizations.xml to configure a Persistence object which it uses to customize the generated classes. If you scroll the XML to find the entry for "3.2.5 xsd:double" you will see the origin of the precision and scale bug. This entry refers to Section 3.2.5 of XML Schema Part 2: Datatypes Second Edition (2004). That section shows that the precision and scale facets are not defined for double (or float).

    Thank you for bringing this to my attention. I'll review and correct DefaultCustomizations.xml for the next release. If you add an issue for this you can keep track of the progress (and remind me to do the review!).

    -- Rick