本文地址 http://blog.csdn.net/duqian42707/article/details/52852859 转载请注明出处。
oracle spatial相关参考文章:
http://blog.csdn.net/duqian42707/article/details/52842001
参考官网: http://www.hibernatespatial.org/
下载需要的jar包:hibernate-spatial-4.0.1.jar、jts-1.13.jar
- 表结构
CREATE TABLE mylake (
feature_id NUMBER PRIMARY KEY,
name VARCHAR2(32),
shape MDSYS.SDO_GEOMETRY);
注意:字段shape为空间字段。
- java bean
import com.vividsolutions.jts.geom.Point;
import org.hibernate.annotations.Type;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Created by dqwork on 2016/10/17.
*/
@Entity
@Table(name = "MYLAKE")
public class Mylake {
private Long featureId;
private String name;
private Point shape;
@Id
@Column(name = "FEATURE_ID")
public Long getFeatureId() {
return featureId;
}
public void setFeatureId(Long featureId) {
this.featureId = featureId;
}
@Column(name = "NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "SHAPE",columnDefinition = "MDSYS.SDO_GEOMETRY")
@Type(type="org.hibernate.spatial.GeometryType")
public Point getShape() {
return shape;
}
public void setShape(Point shape) {
this.shape = shape;
}
}
- 修改hibernate配置文件
hibernate.dialect=org.hibernate.spatial.dialect.oracle.OracleSpatial10gDialect
hibernate.spatial.connection_finder=com.xxx.web.C3P0ConnectionFinder
hibernate.spatial.ogc_strict=false
关于此配置的解释请参考:http://www.hibernatespatial.org/documentation/03-dialects/05-oracle/
- 建立自定义类实现ConnectionFinder
import com.mchange.v2.c3p0.C3P0ProxyConnection;
import oracle.jdbc.driver.OracleConnection;
import org.hibernate.spatial.dialect.oracle.ConnectionFinder;
import org.hibernate.spatial.helper.FinderException;
import org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor;
import java.sql.Connection;
public class C3P0ConnectionFinder implements ConnectionFinder {
/**
* try to get the oracle connection from C3P0Connection
*/
public Connection find(Connection con) throws FinderException {
if (con == null) {
return null;
}
if (con instanceof C3P0ProxyConnection) {
try {
C3P0NativeJdbcExtractor c3P0NativeJdbcExtractor = new C3P0NativeJdbcExtractor();
OracleConnection oracleConnection = (OracleConnection) c3P0NativeJdbcExtractor.getNativeConnection(con);
return oracleConnection;
} catch (Exception e) {
e.printStackTrace();
}
}
throw new FinderException("Couldn't get at the OracleSpatial Connection object from the PreparedStatement.");
}
}
- Dao层实现类
package com.xxx.dao.test.impl;
import com.xxx.dao.hbm.test.Mylake;
import com.xxx.dao.test.IMyLakeDao;
import com.xxx.hibernate.BaseDao;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created by dqwork on 2016/10/17.
*/
@Repository
public class MyLakeDao extends BaseDao implements IMyLakeDao {
public Mylake save(Mylake entity,String wktPoint) {
Geometry geom = wktToGeometry(wktPoint);
if (!geom.getGeometryType().equals("Point")) {
throw new RuntimeException("Geometry must be a point. Got a " + geom.getGeometryType());
}
entity.setShape((Point) geom);
return (Mylake) super.save(entity);//调用hibernate的save()方法
}
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;
}
}
- 逻辑层调用保存方法
Mylake mylake = new Mylake();
mylake.setFeatureId(123L);
mylake.setName("new lake");
myLakeDao.save(mylake,"POINT(10 5)");//注意第二个参数的格式