geotools - 使用 JDBCDataStore 插入新的 SimpleFeature 时是否可以跳过列

geotools - Is it possible to skip columns when inserting new SimpleFeature using JDBCDataStore

在我们的应用程序(Java、Geotools 25、PostGIS)中,我们有表单,用户可以在其中输入一些数据,这些数据将用于创建新功能。在数据库 (PostGIS) 中,列数多于字段数,因此有些列为空。对于其中一些 'empty' 列,数据库中定义了默认值。然而,Geotools 似乎总是为这些列插入空值。

在代码中我有一个 SimpleFeatureStore 并且我根据用户输入创建了一个 SimpleFeature。当我插入新功能 (store.addFeatures()) 时,Geotools 创建的最终 SQL 是一个 INSERT INTO 语句,其中包含所有列的 null 值,这些值不是 'in the form',但其中一些在数据库中有默认值。由于 Geotools 明确地将值设置为 null,因此从不使用默认值。

是否有修复或解决方法我可以尝试解决此问题,以便在未提供任何值时让数据库插入默认值?

DataUtilities class 提供了一个 defaultValues(SimpleFeatureType ) 方法,您可以调用该方法来获取 PropertyDescriptor 中定义的值(尽管默认情况下这是 null)。您可以在 SimpleFeatureType.

中为任何或所有属性定义默认值

我会使用一些代码,例如:

SimpleFeatureTypeBuilder sftb = new SimpleFeatureTypeBuilder();
sftb.setName("test");
sftb.add("id", Integer.class);
sftb.defaultValue("String");
sftb.add("desc", String.class);
SimpleFeatureType schema = sftb.buildFeatureType();
SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(schema);
sfb.addAll(DataUtilities.defaultValues(schema));
sfb.set("id", 23);
SimpleFeature f = sfb.buildFeature("test.1");
System.out.println(f);

产生如下特征:

SimpleFeatureImpl:test=[SimpleFeatureImpl.Attribute: id<id id=test.1>=23,
 SimpleFeatureImpl.Attribute: desc<desc id=test.1>=String]