简体   繁体   English

使用jaxb,hyperjaxb3,jpa2,jts,hibernate-spatial,当hyperjaxb将JTS Point getter标记为@Transient时,postgis失败

[英]using jaxb, hyperjaxb3, jpa2, jts, hibernate-spatial, postgis fails when hyperjaxb marks the JTS Point getter as @Transient

My coworkers lament my adherence to code generation, but I'm determined to prove this particular chain can be done. 我的同事哀叹我坚持代码生成,但我决心证明这个特定的链可以完成。 Examples below are simplified from my project (and thus untested as typed); 下面的例子从我的项目中简化(因此未经测试的类型); I will create a test harness soon. 我很快就会创建一个测试工具。 Currently using hibernate 4.1.4, hibernate-spatial 1.1.1, hyperjaxb3-ejb-plugin 0.5.6. 目前使用的是hibernate 4.1.4,hibernate-spatial 1.1.1,hyperjaxb3-ejb-plugin 0.5.6。

I start off with a schema that uses a Position element whose type is a simple string extension: 我从一个使用Position元素的模式开始,该元素的类型是一个简单的字符串扩展:

  <xsd:simpleType name="wktPoint">
    <xsd:restriction base="xsd:string">
    </xsd:restriction>
  </xsd:simpleType>

I use bindings.xjb customizations to map my xmlType wktPoint to a JTS Point javaType using jaxb:javaType, since I will want to map the JTS Point field to a postgis geometry column: 我使用bindings.xjb自定义来使用jaxb:javaType将我的xmlType wktPoint映射到JTS Point javaType,因为我想将JTS Point字段映射到postgis几何列:

<jaxb:javaType name="com.vividsolutions.jts.geom.Point" xmlType="wktPoint" parseMethod="test.Reader.readWKTPoint" printMethod="test.Writer.writeWKTPoint" />

Later in the bindings.xjb, I start customizing the Position element using annox:annotation: 稍后在bindings.xjb中,我开始使用annox:annotation自定义Position元素:

<jaxb:bindings node="xsd:complexType[@name='MyType']//xsd:element[@name='Position']">
  <annox:annotate target="getter">
    <annox:annotate annox:class="javax.persistence.Basic"/>        
    <annox:annotate annox:class="javax.persistence.Column" name="POSITION" columnDefinition="GEOMETRY"/>
    <annox:annotate annox:class="org.hibernate.annotations.Type" type="org.hibernatespatial.GeometryUserType">
      <annox:annotate annox:field="parameters">
        <annox:annotate annox:class="org.hibernate.annotations.Parameter" name="dialect" value="postgis"/>
      </annox:annotate>
    </annox:annotate>
  </annox:annotate>      
</jaxb:bindings>

However, this produces the following getPosition method in MyType.java: 但是,这会在MyType.java中生成以下getPosition方法:

/**
 * Gets the value of the position property.
 * @return
 *     possible object is
 *     {@link String }
 */
@Transient
@Basic
@Column(columnDefinition = "GEOMETRY", name = "POSITION")
@Type(parameters = {
    @Parameter(name = "dialect", value = "postgis")
}, type = "org.hibernatespatial.GeometryUserType")
public Point getPosition() {
    return position;
}

So when I start up my application and jpa/hibernate starts initializing the tables, it skips the POSITION column altogether (since it got marked @Transient). 因此,当我启动我的应用程序并且jpa / hibernate开始初始化表时,它会完全跳过POSITION列(因为它标记为@Transient)。

How can I prevent @Transient from showing up? 如何防止@Transient出现? It seems I need to convince Hyperjaxb that we will be able to write the JTS Point type directly to the database (that's what all the @Type stuff is about, signaling that we want hibernatespatial to use the postgis dialect when reading and writing Positions). 我似乎需要说服Hyperjaxb,我们将能够直接将JTS Point类型写入数据库(这就是所有@Type的内容,表明我们希望hibernatespatial在读取和写入Position时使用postgis方言)。 Any ideas on how to do that? 有关如何做到这一点的任何想法?

The 0.5.6 hyperjaxb-ejb-plugin just doesn't handle the binding customization properly. 0.5.6 hyperjaxb-ejb-plugin只是没有正确处理绑定定制。 I built a 0.5.7-SNAPSHOT that modified the org.jvnet.hyperjaxb3.ejb.strategy.mapping AttributesMapping.getAttributeMapping() so that my customized jaxb binding would no longer be labelled Transient. 我构建了一个0.5.7-SNAPSHOT,它修改了org.jvnet.hyperjaxb3.ejb.strategy.mapping AttributesMapping.getAttributeMapping(),这样我的自定义jaxb绑定就不再被标记为Transient。 I gave it the following fallback check: 我给了它以下的后备检查:

if (isFieldOutlineBasic(fieldOutline)) {
    ...
} else if (isFieldOutlineComplex(fieldOutline)) {
    ...
} else {
    if (fieldOutline.getRawType() instanceof JClass) {
        return context.getBasicMapping();
    }
}

This covered my customization, which would always show up as a JDirectClass(Point). 这涵盖了我的自定义,它将始终显示为JDirectClass(Point)。 Once I made that change, the postgis tables were created with the correct geometry types, and I can now take in a WKT Point string, have it unmarshall as a JTS Point, and then persist it with JPA as a PostGIS geometry. 一旦我做了这个改变,postgis表就是用正确的几何类型创建的,现在我可以接受一个WKT Point字符串,让它作为JTS Point解组,然后用JPA作为PostGIS几何体保存它。 I did also need to rebuild Hibernate Spatial (now using a 1.1.2-SNAPSHOT) to work with Hibernate4. 还需要重建Hibernate的空间(现在用的是1.1.2-SNAPSHOT)与Hibernate4工作。 I'll submit this work to Karel Maesen and I'll also submit the Hyperjaxb change to Aleksei Valikov and hopefully future versions will be that much more robust. 我将这项工作提交给Karel Maesen,我还将向Aleksei Valikov提交Hyperjaxb更改,希望未来的版本将更加强大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM