2

I am using geotools (v 17.0) to connect to a H2 database (v 1.4.193) table. My h2 maven dependencies:

<dependency>
    <groupId>org.geotools.jdbc</groupId>
    <artifactId>gt-jdbc-h2</artifactId>
    <version>17.0</version>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.193</version>
</dependency>

The H2 database is running in server mode. I set up my table like this (using the browser based console):

CREATE SCHEMA IF NOT EXISTS mySchema;
CREATE TABLE mySchema.point
(
    the_id NUMBER PRIMARY KEY,
    the_geom GEOMETRY
);
CREATE SPATIAL INDEX spatial_idx_point ON mySchema.point(the_geom);

My database connection:

// connection parameter
Map<String,Object> params = new HashMap();
params.put( "dbtype", "h2");
params.put( "host", "myHost");
//params.put( "schema", "mySchema");
params.put( "port", 9092);
params.put( "database", "myDB");
params.put( "user", "sa");
params.put( "passwd", "");

// create connection
DataStore dataStore = DataStoreFinder.getDataStore(params);
System.out.println("dataStore.getNames() -> " + dataStore.getNames());
FeatureSource featureSource = dataStore.getFeatureSource("POINT");
FeatureStore featureStore = (FeatureStore) featureSource;

With the schema parameter enabled I get this error:

java.io.IOException: Schema 'POINT' does not exist. at org.geotools.data.store.ContentDataStore.ensureEntry(ContentDataStore.java:620) at org.geotools.data.store.ContentDataStore.getFeatureSource(ContentDataStore.java:393) at org.geotools.data.store.ContentDataStore.getFeatureSource(ContentDataStore.java:360) at org.geotools.data.store.ContentDataStore.getFeatureSource(ContentDataStore.java:103) at db_connector.H2Connection.setFeatureStore(H2Connection.java:49) at db_connector.DbConnection.(DbConnection.java:25) at db_connector.H2Connection.(H2Connection.java:17) at feature.FeatureGenerationStarter.main(FeatureGenerationStarter.java:59) Exception in thread "main" java.lang.NullPointerException at db_connector.DbConnection.insertFeatures(DbConnection.java:53) at feature.FeatureGenerationStarter.main(FeatureGenerationStarter.java:60)

The pritln results in:

dataStore.getNames() -> []

Without the schema parameter enabled I get the following error:

2017-04-25T13:27:55.441+0200 WARNING Failure occurred while looking up the primary key with finder: org.geotools.jdbc.HeuristicPrimaryKeyFinder@27912e3 org.h2.jdbc.JdbcSQLException: Tabelle "POINT" nicht gefunden Table "POINT" not found; SQL statement: SELECT "THE_ID" FROM "POINT" WHERE 0=1 [42102-193] at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) at org.h2.message.DbException.get(DbException.java:179) at org.h2.message.DbException.get(DbException.java:155) at org.h2.command.Parser.readTableOrView(Parser.java:5389) at org.h2.command.Parser.readTableFilter(Parser.java:1257) at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1897) at org.h2.command.Parser.parseSelectSimple(Parser.java:2045) at org.h2.command.Parser.parseSelectSub(Parser.java:1891) at org.h2.command.Parser.parseSelectUnion(Parser.java:1709) at org.h2.command.Parser.parseSelect(Parser.java:1697) at org.h2.command.Parser.parsePrepared(Parser.java:445) at org.h2.command.Parser.parse(Parser.java:317) at org.h2.command.Parser.parse(Parser.java:289) at org.h2.command.Parser.prepareCommand(Parser.java:254) at org.h2.engine.Session.prepareLocal(Session.java:561) at org.h2.server.TcpServerThread.process(TcpServerThread.java:263) at org.h2.server.TcpServerThread.run(TcpServerThread.java:158) at java.lang.Thread.run(Unknown Source)

at org.h2.engine.SessionRemote.done(SessionRemote.java:624) at org.h2.command.CommandRemote.prepare(CommandRemote.java:68) at org.h2.command.CommandRemote.(CommandRemote.java:45) at org.h2.engine.SessionRemote.prepareCommand(SessionRemote.java:494) at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1203) at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:73) at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208) at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208) at org.geotools.jdbc.HeuristicPrimaryKeyFinder.createPrimaryKey(HeuristicPrimaryKeyFinder.java:140) at org.geotools.jdbc.HeuristicPrimaryKeyFinder.getPrimaryKey(HeuristicPrimaryKeyFinder.java:59) at org.geotools.jdbc.CompositePrimaryKeyFinder.getPrimaryKey(CompositePrimaryKeyFinder.java:52) at org.geotools.jdbc.JDBCDataStore.getPrimaryKey(JDBCDataStore.java:1109) at org.geotools.jdbc.JDBCFeatureSource.(JDBCFeatureSource.java:95) at org.geotools.jdbc.JDBCDataStore.createFeatureSource(JDBCDataStore.java:955) at org.geotools.data.store.ContentDataStore.getFeatureSource(ContentDataStore.java:395) at org.geotools.data.store.ContentDataStore.getFeatureSource(ContentDataStore.java:360) at org.geotools.data.store.ContentDataStore.getFeatureSource(ContentDataStore.java:103) at db_connector.H2Connection.setFeatureStore(H2Connection.java:49) at db_connector.DbConnection.(DbConnection.java:25) at db_connector.H2Connection.(H2Connection.java:17) at feature.FeatureGenerationStarter.main(FeatureGenerationStarter.java:59)

2017-04-25T13:27:55.461+0200 WARNING Could not find mapping for 'THE_GEOM', ignoring the column and setting the feature type read only Exception in thread "main" java.lang.ClassCastException: org.geotools.jdbc.JDBCFeatureSource cannot be cast to org.geotools.data.FeatureStore at db_connector.H2Connection.setFeatureStore(H2Connection.java:51) at db_connector.DbConnection.(DbConnection.java:25) at db_connector.H2Connection.(H2Connection.java:17) at feature.FeatureGenerationStarter.main(FeatureGenerationStarter.java:59)

The println results in:

dataStore.getNames() -> [POINT, SPATIAL_REF_SYS]

I had a similar problem with Postgis a while ago where the spatial index had to be adjusted like:

CREATE INDEX spatial_idx_point ON mySchema.point USING GIST(geom);

Is there something similar for H2? Any suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.