在Hibernate中支援Oracle Spatial的配置
阿新 • • 發佈:2019-01-31
如今因為工作需要,來學習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配置如下:
接下來就是Hibernate的配置檔案了,要支援Hibernate Spatial只需要改變方言(dialect )的配置,其他資訊不用改變。Hibernate Spatial繼承了Hibernate的Dialects,以便於HQL和JPQL能支援空間函式,下面我們在看看幫助文件上面的配置檔案:<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>
其他資料庫的dialect如下:<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>
- 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的格式。