NutzCN Logo
问答 问一个java问题,h2存取geometry类型数据读写操作?
发布于 2241天前 作者 wx_jjn2ia9qvotogktb45bf 2264 次浏览 复制 上一个帖子 下一个帖子
标签:

h2数据库存取geometry字段,根据@ColDefine的Adapter属性实体类该怎么重写get和set属性?

12 回复

adapter是自定义ValueAdapter实现类,自行实现 java属性<-->数据库字段 的操作

能不能具体一点,我在网上找的oracle的geometry字段的转换方式,模仿下面这个,写的报类型转换错误

public abstract class GeoDataAdaptor implements ValueAdaptor {
    @Override
    public Object get(ResultSet rs, String colName) throws SQLException{
        STRUCT obj = (STRUCT)rs.getObject(colName);
        JGeometry j_geom = JGeometry.load(obj);

        return j_geom;
    }

    @Override
    public void set(PreparedStatement stat, Object obj, int index) throws SQLException{
        JGeometry j_geom = (JGeometry)obj;
        STRUCT dbObject;
        try {
            Connection conn = stat.getConnection().getMetaData().getConnection();
            dbObject = JGeometry.store((OracleConnection)conn, j_geom);
            stat.setObject(index, dbObject);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public abstract Class<?> GeoClassType();
}

这个是我写的

@Override
    public Object get(ResultSet rs, String colName) throws SQLException {
        //H2得到geo类型
        SpatialResultSet spatialResultSet =(SpatialResultSet) rs.getObject(colName);
        Geometry geometry =(Geometry)spatialResultSet;
        return null;
    }

把return的返回值改成geometry

(OracleConnection)conn

有连接池的话,conn不会是原始的连接类

@wendal
我看api重新写了一下

public Object get(ResultSet rs, String colName) throws SQLException {
        //H2得到geo类型

      
        SpatialResultSet srs = (SpatialResultSet)rs;
        Geometry geometry = srs.getGeometry(colName);

        return geometry;
    }

报错

  For example:> "SELECT * FROM DILI  WHERE CATEGORYID='800001'"
[DEBUG] 2018-09-27 17:40:09,599 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) - [POST]URI=/platform/sys/log/data 171639ms
[WARN ] 2018-09-27 17:40:11,788 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) - Error@/platform/sys/log/data :
org.nutz.dao.DaoException: java.lang.ClassCastException: com.alibaba.druid.pool.DruidPooledResultSet cannot be cast to org.h2gis.utilities.SpatialResultSet
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:140)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:93)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:82)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:252)
	at org.nutz.dao.impl.NutDao.query(NutDao.java:483)
	at org.nutz.dao.impl.NutDao.query(NutDao.java:488)

是不是连接池的问题

DruidPooledResultSet  rs2 = (DruidPooledResultSet)rs;
SpatialResultSet srs = (SpatialResultSet)rs2.getRawResultSet();

根据您的修改意见

[DEBUG] 2018-09-27 17:57:09,673 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) - [POST]URI=/platform/sys/log/data 4335ms
[WARN ] 2018-09-27 17:57:09,704 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) - Error@/platform/sys/log/data :
org.nutz.dao.DaoException: java.lang.ClassCastException: org.h2.jdbc.JdbcResultSet cannot be cast to org.h2gis.utilities.SpatialResultSet
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:140)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:93)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:82)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:252)
	at org.nutz.dao.impl.NutDao.query(NutDao.java:483)
	at org.nutz.dao.impl.NutDao.query(NutDao.java:488)
	at cn.wizzer.app.web.modules.controllers.platform.sys.SysLogController.data(SysLogController.java:90)
	at cn.wizzer.app.web.modules.controllers.platform.sys.SysLogController$FM$data$50937fad7db7cae8a302fd843c05f99f.invoke(SysLogController.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:38)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:35)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.NutShiroProcessor.process(NutShiroProcessor.java:52)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.GlobalsSettingProcessor.process(GlobalsSettingProcessor.java:48)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:21)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at cn.wizzer.app.web.commons.filter.RouteFilter.doFilter(RouteFilter.java:31)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2521)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2510)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

还是类型转换的问题

SpatialResultSet是什么情况下才会返回的?

http://www.h2gis.org/docs/1.3.2/spatial-jdbc/

看上去数据源也得改造一下,才能有那个结果类型

@wendal 我是根据网上的H2教程,这段代码,看到其geometry的返回值类型,然后在get方法里面试

SpatialResultSet rs = st.executeQuery(
    "SELECT the_geom FROM mygeomtable").
        unWrap(SpatialResultSet.class);
rs.next();
Geometry myGeom = rs.getGeometry("the_geom");

你还是看官网文档吧,在main方法下实验ok了,再集成进nutz

添加回复
请先登陆
回到顶部