【问题标题】:Adding columnDefinition property to Hyperjaxb generated @Column attribute将 columnDefinition 属性添加到 Hyperjaxb 生成的 @Column 属性
【发布时间】:2015-10-05 18:17:29
【问题描述】:

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

<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 实现类似的功能呢?

【问题讨论】:

    标签: java mysql hibernate jpa hyperjaxb


    【解决方案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 中。然后它们将覆盖默认映射。

    【讨论】:

    • 完美。正是我需要的。谢谢。很棒的工具,hyperjaxb,顺便说一句。
    猜你喜欢
    • 2013-03-01
    • 2013-04-11
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 2013-09-02
    • 1970-01-01
    相关资源
    最近更新 更多