hibernate spatial - 保存oracle具有空间字段的表
2016-10-18 21:16
337 查看
本文地址 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
表结构
注意:字段shape为空间字段。
java bean
修改hibernate配置文件
关于此配置的解释请参考:http://www.hibernatespatial.org/documentation/03-dialects/05-oracle/
建立自定义类实现ConnectionFinder
Dao层实现类
逻辑层调用保存方法
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)");//注意第二个参数的格式
相关文章推荐
- 如何在oracle的blob字段里保存多个文件
- Oracle 添加字段后不在同一个表空间造成select很慢。
- oracle中修改已有字段数据类型为clob的方法以及ssh框架中如何保存clob字段
- 将 C# 的图像对象保存到 Oracle BLOB 字段中
- ASP.NET 在Oracle中CLOB字段中保存并读取图片
- oracle 中导入数据(含Lob字段)到不同表空间的问题
- Oracle 空间字段 创建索引
- 根据标题后缀名传参点击下载保存内容为任意格式文件(内容为oracle库中某字段值:BLOB类型)
- oracle临时表空间 ORA-01652:无法通过16(在表空间XXX中)扩展 temp 字段
- Kettle 生成的XML格式数据保存到Oracle 数据库的BLOB类型字段中
- oracle创建表空间、用户、授权、修改字段长度、导出、导入等
- 关于文件保存到Oracle中BLOB字段的方法及例子
- Java保存yyyy-MM-dd HH:mm:ss日期字符串到 oracle date类型字段[原]
- java将文件保存为二进制流到oracle表中的blob字段
- oracle中查询表空间中的表 表中的字段 视图的脚本
- 如何在oracle的blob字段里保存多个文件
- oracle 构造类似desc命令和查看表中字段所占空间
- Oracle 查看用户,表,表空间,字段
- oracle 怎样查询含blob字段的表所占空间的大小
- Kettle 生成的XML格式数据保存到Oracle 数据库的BLOB类型字段中