将columnDefinition属性添加到Hyperjaxb生成的@Column属性

时间:2015-10-05 18:17:29

标签: java mysql hibernate jpa hyperjaxb

我正在使用Hyperjaxb生成我的JPA映射。然后我使用hibernate3-maven-plugin生成数据库的SQL脚本。我的问题在于我有一个类型具有如下定义的属性:

<xsd:element name="priority" type="xsd:boolean"/>

sql脚本定义了这样的列

PRIORITY bit,

JPA实体就像这样定义它

/**
 * Obtient la valeur de la propriété priority.
 * 
 */
@Basic
@Column(name = "PRIORITY")
public boolean isPriority() {
    return priority;
}

/**
 * Définit la valeur de la propriété priority.
 * 
 */
public void setPriority(boolean value) {
    this.priority = value;
}

我使用MySql作为后端。当我的JPA / Hibernate entityManager尝试针对数据库验证我的JPA模型时,问题就出现了。然后我收到此错误

org.hibernate.HibernateException: Wrong column type in custom.sample_type for column PRIORITY. Found: bit, expected: boolean

如何修复此错误?在某处,我读到了我可以在java代码中做这样的事情

@Basic
@Column(name = "B", columnDefinition = "BIT", length = 1)
public boolean isB() {
    return b;
}

但是我的JPA java代码是由Hyperjaxb自动生成的,那么如何用Hyperjaxb实现类似的东西?

1 个答案:

答案 0 :(得分:1)

免责声明:我是Hyperjaxb的作者。

我尝试使用以下方式自定义您的媒体资源:

<hj:basic>
  <orm:column column-definition="..."/>
</hj:basic>

请参阅customizations schema及其使用的ORM schema

如果您不想自定义每个布尔值(您可能不想要),也可以配置每种类型的自定义:

<hj:default-single-property type="xsd:boolean">
    <hj:basic>
        <orm:column column-definition="..."/>
    </hj:basic>
</hj:default-single-property>   

<hj:default-collection-property type="xsd:boolean">
    <hj:element-collection>
        <orm:column column-definition="..."/>
    </hj:element-collection>
</hj:default-collection-property>

请参阅绑定文件的this example

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
    version="2.1"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:hj="http://hyperjaxb3.jvnet.org/ejb/schemas/customizations"
    xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:annox="http://annox.dev.java.net"
    jaxb:extensionBindingPrefixes="hj orm annox">

    <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="org.jvnet.hyperjaxb3.ejb.tests.pocustomized"/>
        </jaxb:schemaBindings>
        <hj:persistence>
            <hj:default-generated-id name="MySuperId" transient="true">
                <orm:column name="MY_SUPER_ID"/>
            </hj:default-generated-id>
            <hj:default-one-to-many>
                <orm:join-table/>
            </hj:default-one-to-many>
        </hj:persistence>
        <jaxb:bindings node="xs:complexType[@name='one']/xs:sequence/xs:element[@name='many-to-many-join-table']">
            <annox:annotate>
                <annox:annotate annox:class="org.hibernate.annotations.Cascade" value="DELETE_ORPHAN"/>
            </annox:annotate>
        </jaxb:bindings>

        <jaxb:bindings node="xs:element[@name='ten']/xs:complexType">
            <hj:basic name="content">
                <orm:column length="1024"/>
            </hj:basic>
        </jaxb:bindings>

    </jaxb:bindings>


</jaxb:bindings>

您必须将hj:default-...-property元素放在hj:persistence内。然后,它们将覆盖默认映射。