2015-10-05 86 views
0

我正在使用Hyperjaxb生成我的JPA映射。然后我使用hibernate3-maven-plugin生成数据库的SQL脚本。我的问题就在于,我有一个具有像这样定义的属性类型:将columnDefinition属性添加到Hyperjaxb生成的@Column属性

<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

免责声明:我是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之内。然后他们将覆盖默认映射。

+0

完美。正是我需要的。谢谢。伟大的工具,hyperjaxb,顺便说一句。 –