一、读取Geometry对象
在java映射的数据库表实体类对象的字段中添加Geometry类型,如下:
@Column(name = "GEOM")
private Geometry geom;
这样就能读取到Geometry 对象,Geometry 有很多空间计算方法,具体可以查看源码。
例:
1.实体类:
import org.hibernate.annotations.Type;
import com.vividsolutions.jts.geom.Polygon;
/**
* Shape()
*/
//@Column(name = "Shape", jdbcType = JdbcType.STRUCT)
@Type(type = "org.hibernate.spatial.GeometryType")
@Column(name = "Shape")
private Polygon Shape;
public Polygon getShape() {
return Shape;
}
public void setShape(Polygon shape) {
Shape = shape;
}
二、写入Geometry对象
相对于读取,写入就相对麻烦一些,首先要构建一个Geometry对象,分点线面的构建:
private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
点的构建
Envelope e = new Envelope(new Coordinate(x, y));
线的构建
LineString line = geometryFactory.createLineString(Coordinate[]);
line.setSRID(20131028);
rb.setGeometry(line);
其中,rb实体类对象的Geometry的定义格式如下:
@Column(name = "PKG_CONVEX", jdbcType = JdbcType.STRUCT)
private Geometry geometry;
一定要指定jdbcType 类型,Oracle数据库中Geometry对象只接收STRUCT类型的数据
三、*** hibernate注解报错注解方式下报java.lang.UnsupportedOperationException
hibernate 不支持此类型输出
结局方法,配置hibernate方言
例如Sqlserver 数据库
四、Hibernate中的数据库方言(Dialect)
在配置hibernate.cfg.xml时需指定使用数据库的方言:
例:
<property name="dialect">org.hibernate.dialect.MySQL5Dialectproperty>
以下是各数据库对应的方言(Dialect):
数据库 |
方言(Dialect) |
DB2 |
org.hibernate.dialect.DB2Dialect |
DB2 AS/400 |
org.hibernate.dialect.DB2400Dialect |
DB2 OS390 |
org.hibernate.dialect.DB2390Dialect |
PostgreSQL |
org.hibernate.dialect.PostgreSQLDialect |
MySQL5 |
org.hibernate.dialect.MySQL5Dialect |
MySQL5 with InnoDB |
org.hibernate.dialect.MySQL5InnoDBDialect |
MySQL with MyISAM |
org.hibernate.dialect.MySQLMyISAMDialect |
Oracle(any version) |
org.hibernate.dialect.OracleDialect |
Oracle 9i |
org.hibernate.dialect.Oracle9iDialect |
Oracle 10g |
org.hibernate.dialect.Oracle10gDialect |
Oracle 11g |
org.hibernate.dialect.Oracle10gDialect |
Sybase |
org.hibernate.dialect.SybaseASE15Dialect |
Sybase Anywhere |
org.hibernate.dialect.SybaseAnywhereDialect |
Microsoft SQL Server 2000 |
org.hibernate.dialect.SQLServerDialect |
Microsoft SQL Server 2005 |
org.hibernate.dialect.SQLServer2005Dialect |
Microsoft SQL Server 2008 |
org.hibernate.dialect.SQLServer2008Dialect |
SAP DB |
org.hibernate.dialect.SAPDBDialect |
Informix |
org.hibernate.dialect.InformixDialect |
HypersonicSQL |
org.hibernate.dialect.HSQLDialect |
H2 Database |
org.hibernate.dialect.H2Dialect |
Ingres |
org.hibernate.dialect.IngresDialect |
Progress |
org.hibernate.dialect.ProgressDialect |
Mckoi SQL |
org.hibernate.dialect.MckoiDialect |
Interbase |
org.hibernate.dialect.InterbaseDialect |
Pointbase |
org.hibernate.dialect.PointbaseDialect |
FrontBase |
org.hibernate.dialect.FrontbaseDialect |
Firebird |
org.hibernate.dialect.FirebirdDialect |
5.hibernate配置模板
(1)test-applicationContext-jpa.xml
Spring Data JPA
${hibernate.hbm2ddl.auto}
${hibernate.dialect}
org.hibernate.cache.ehcache.EhCacheRegionFactory
test-ehcache-hibernate-local.xml
org.hibernate.cfg.ImprovedNamingStrategy
(2)test-ehcache-hibernate-local.xml
(3)logback-test.xml
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
(4)
# Hibernate
hibernate.hbm2ddl.auto=update
hibernate.generateDdl=true
hibernate.showSql=true
hibernate.dialect=org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect
#===================================================
#MSSqlServer database settings
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:jtds:sqlserver://****:1433;DatabaseName=***
jdbc.username=sa
jdbc.password=123456
如果有问题,请在下方评论
想获得更多的学习知识请关注微信公众号:西北码农或扫下方二维码