在Hibernate中支持Oracle Spatial的配置
如今因为工作需要,来学习Oracle Spatial,因为先使用Hibernate,所以想使用Hibernate对空间数据库的支持,于是找到了HibernateSpatial。
Hibernate Spatial版本与Hibernate对应如下:
- Hibernate Spatial version 1.0 is compatible with Hibernate 3.2.x – 3.5.x
- Hibernate Spatial version 1.1.x is compatible with Hibernate 3.6.x
- Hibernate Spatial version 4.0 is compatible with Hibernate 4.x
要想使用Hibernate Spatial还需要添加JST,JST从根本上而言其实本不是很复杂,它主要是完成了Java对几何对象、空间拓扑的核心操作算法。
jar包如下:
jts-1.8.jar
jtsio-1.8.jar
如果使用Mavan则就方便了,pom.xml配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.hibernate.spatial.tutorials</groupId>
<artifactId>event-tutorial</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>event-tutorial</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>4.0.0.Final</hibernate.version>
</properties>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Hibernate Spatial -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>4.0-M1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- the postgresql driver -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-701.jdbc3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.11</version>
</dependency>
</dependencies>
<!-- add repositories for JTS and Hibernate Spatial and Hibernate (JBoss) -->
<repositories>
<repository>
<id>OSGEO GeoTools repo</id>
<url>http://download.osgeo.org/webdav/geotools</url>
</repository>
<repository>
<id>Hibernate Spatial repo</id>
<url>http://www.hibernatespatial.org/repository</url>
</repository>
<!-- add JBOSS repository for easy access to Hibernate libraries -->
<repository>
<id>JBOSS</id>
<url>https://repository.jboss.org/nexus/content/repositories/releases/</url>
</repository>
</repositories>
</project>
接下来就是Hibernate的配置文件了,要支持Hibernate Spatial只需要改变方言(dialect )的配置,其他信息不用改变。Hibernate Spatial继承了Hibernate的Dialects,以便于HQL和JPQL能支持空间函数,下面我们在看看帮助文档上面的配置文件:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="org.hibernate.events.jpa" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.spatial.dialect.postgis.PostgisDialect"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432:hstutorial"/>
<property name="hibernate.connection.username" value="hstutorial"/>
<property name="hibernate.connection.password" value="hstutorial"/>
<property name="hibernate.connection.pool_size" value="5"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.max_fetch_depth" value="5"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
其他数据库的dialect如下:
- Oracle 10g/11g: org.hibernate.spatial.dialect.oracle.OracleSpatial10gDialect
- MySQL 5+: org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect or org.hibernate.spatial.dialect.mysql.MySQLSpatialInnoDBDialect
- Microsoft SQL Server 2008 : org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect
- H2 + GeoDB: org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
下面是实体类:
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import com.vividsolutions.jts.geom.Geometry;
/**
*
* @author Oliver
*
*/
@Entity
@Table(name = "DZXXB")
public class Address {
private String uuid;
private Geometry shape;
private Integer type;
private Integer status;
@Id
@Column(name="DZBZ")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
@Column(name = "DZKJWZ")
@Type(type="org.hibernate.spatial.GeometryType")
public Geometry getShape() {
return shape;
}
public void setShape(Geometry shape) {
this.shape = shape;
}
@Column(name = "DZLX")
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
@Column(name = "DZZT")
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
注意@Type,它是Hibernate的注解,它表示所标识的属性为几何体类型(Geometry Type)。
到这里,实体类定义好了,数据库中的表也已经建立好了,只需要写上DAO层就OK了。问题就在于怎么初始化Geometry的对象呢?帮助文档上面说明了,只需要提供一个Well-Known Text (WKT)格式的字符串,再通过WKTReader的read(String)方法就可以将WKT文本转化成Geometry对象了。代码如下:
private Geometry wktToGeometry(String wktPoint) {
WKTReader fromText = new WKTReader();
Geometry geom = null;
try {
geom = fromText.read(wktPoint);
} catch (ParseException e) {
throw new RuntimeException("Not a WKT string:" + wktPoint);
}
return geom;
}
既然已经产生了Geometry的对象,然后就可以按照一般Hibernate的CRUD继续编程了,至于Geometry的使用,可以查看API。下一篇文章会说明WKT的格式。
转载自:https://blog.csdn.net/zhangzz1127/article/details/10263363