googleapis / java-spanner-jdbc

Apache License 2.0
64 stars 48 forks source link

[Spanner JDBC] Spanner JSON type can not be null using JPA + Hibernate #761

Closed suztomo closed 1 year ago

suztomo commented 2 years ago

We observe that google-cloud-spanner-jdbc doesn't let us set null for JSON column. From the stacktrace below, it seems that the related classes are from google-cloud-spanner-jdbc. Do you think it's a missing feature in this library?

Environment details

  1. Specify the API at the beginning of the title. For example, "BigQuery: ..."). General, Core, and Other are also allowed as types
  2. OS type and version: Mac (but I believe it's all environment)
  3. Java version: Java 11
  4. version(s): google-cloud-spanner-jdbc 2.5.11 (latest)

Steps to reproduce

  1. Run this test case https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate/pull/383 where it tries to set null to JSON column
  2. Observe the error. It shows java.lang.IllegalArgumentException: Unsupported sql type for setting to null: 100011.

Code example

      JsonEntity jsonEntity = new JsonEntity();
      jsonEntity.setEmployee(null);

      entityManager.persist(jsonEntity);
      entityManager.flush();

      Session session = entityManager.unwrap(Session.class);
      List<JsonEntity> entities =
          session.createQuery(
              "from JsonEntity", JsonEntity.class).list();
      assertThat(entities).hasSizeGreaterThan(0);

      JsonEntity entity = entities.get(0);
      assertThat(entity.getEmployee()).isNull();

From https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate/pull/383/files

Stack trace

java.lang.IllegalArgumentException: Unsupported sql type for setting to null: 100011

    at com.google.cloud.spanner.jdbc.JdbcParameterStore.setNullValue(JdbcParameterStore.java:948)
    at com.google.cloud.spanner.jdbc.JdbcParameterStore.setSingleValue(JdbcParameterStore.java:497)
    at com.google.cloud.spanner.jdbc.JdbcParameterStore.setValue(JdbcParameterStore.java:484)
    at com.google.cloud.spanner.jdbc.JdbcParameterStore.bindParameterValue(JdbcParameterStore.java:466)
    at com.google.cloud.spanner.jdbc.JdbcPreparedStatement.createStatement(JdbcPreparedStatement.java:56)
    at com.google.cloud.spanner.jdbc.JdbcPreparedStatement.addBatch(JdbcPreparedStatement.java:93)
    at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.addToBatch(BatchingBatch.java:78)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3291)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3824)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:107)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
    at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478)

External references such as API reference guides

Any additional information below

We're not 100% certain that google-cloud-spanner-jdbc is the root cause of the error. Do you think this is a missing feature in the library?

https://cloud.google.com/spanner/docs/working-with-json#create_a_table_with_a_json_column says:

JSON columns can be nullable

ansh0l commented 2 years ago

Quick update: Error trace seems to point at JdbcParameterStore, but I don't see anything of the nature in JdbcParameterStore , or in the tests for it. cc @thiagotnunes

suztomo commented 2 years ago

@ansh0l Thank you for checking. I added https://cloud.google.com/spanner/docs/working-with-json#create_a_table_with_a_json_column says:

JSON columns can be nullable

in the issue description. I'll wait for your update.

rajatbhatta commented 1 year ago

This should be fixed in the latest version.