Mapping Geometry in Grails and MySQL

针对地理数据的ORM,有一个Hibernate的扩展HibernateSpatial项目可以将JTS对象映射到MySQL/PostGIS/Oracle中。这个扩展同样可以用在Grails里,这里有一篇简单的介绍,关于在Grails和MySQL中管理地理数据:
http://www.grails.org/MySQL+GIS-Geometry+with+Grails

不过按照这个文章里介绍的方法用,很可能会遭遇这样的报错:

org.hibernate.MappingException: No Dialect mapping for JDBC type: 2003

这个问题最终在这里得到了解答:
http://n2.nabble.com/No-Dialect-mapping-for-JDBC-type-2003-td1141106.html
按照邮件列表里的反映,上面的配置在Postgis里是可以work的,但是如果用Mysql还需要指定JPA的columnDefinition,对应的Hibernate属性是sql-type。虽然作者承诺会在今后的版本里修改这个问题,不过眼下的M2版本还没有修正这个问题。为此,Grails的用户特地提出在Grails中加入sql-type的支持:
http://jira.codehaus.org/browse/GRAILS-3201
现在按照下面文档的说明,可以在mapping里指定sqlType了:
http://grails.org/doc/latest/ref/Database%20Mapping/column.html

实例代码里的domain定义应该改成:

import com.vividsolutions.jts.geom.Polygon
import org.hibernatespatial.GeometryUserType

public class MyPoly {
    String name
    Polygon poly

    static mapping = {
        poly type: GeometryUserType, sqlType:"GEOMETRY"
    }

}

于是,再也没有莫名其妙的No Dialect报错了。

This entry was written by Sunng , posted on Tuesday October 06 2009at 07:10 pm , filed under 手艺 and tagged , , , , . Bookmark the permalink . Post a comment below or leave a trackback: Trackback URL.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word