Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Postgres UUID arrays using JDBC #1567

Closed
rdehuyss opened this issue Jul 19, 2023 · 1 comment
Closed

Add support for Postgres UUID arrays using JDBC #1567

rdehuyss opened this issue Jul 19, 2023 · 1 comment
Assignees
Labels
type: enhancement A general enhancement

Comments

@rdehuyss
Copy link

rdehuyss commented Jul 19, 2023

Hi again,

First of all - let me tell you that I love Spring Data JDBC ❤️! It makes life 🎉!

I noticed that the combination of Spring Data JDBC, Postgres and UUID array type doesn't seem to work. My guess is this is because of the fact that java.sql.JDBCType does not contain an entry for UUID - is that correct?

To reproduce:

schema.sql

create table uuid_test
(
    "id"           uuid,
    "version"      int,
    "to_ids"       uuid[],
);

Record to save

@Table(name="uuid_test")
public record UUIDTest(@Id UUID id, @Version int version,  Set<UUID> toIds) {}

The exception message

Caused by: org.postgresql.util.PSQLException: Unable to find server array type for provided name UNKNOWN.

Workaround

My current workaround is to create the JdbcTypeFactory:

static class PostgresJdbcTypeFactory implements JdbcTypeFactory {
        private final JdbcOperations operations;
        private final JdbcArrayColumns arrayColumns;

        /**
         * Creates a new {@link DefaultJdbcTypeFactory}.
         *
         * @param operations must not be {@literal null}.
         * @since 2.3
         */
        public PostgresJdbcTypeFactory(JdbcOperations operations, JdbcArrayColumns arrayColumns) {
            Assert.notNull(operations, "JdbcOperations must not be null");
            Assert.notNull(arrayColumns, "JdbcArrayColumns must not be null");

            this.operations = operations;
            this.arrayColumns = arrayColumns;
        }

        @Override
        public Array createArray(Object[] value) {

            Assert.notNull(value, "Value must not be null");

            Class<?> componentType = arrayColumns.getArrayType(value.getClass());

            if(UUID.class.equals(componentType)) {
                String typeName = "uuid";
                return operations.execute((ConnectionCallback<Array>) c -> c.createArrayOf(typeName, value));
            } else {
                SQLType jdbcType = JdbcUtil.targetSqlTypeFor(componentType);
                Assert.notNull(jdbcType, () -> String.format("Couldn't determine JDBCType for %s", componentType));
                String typeName = arrayColumns.getArrayTypeName(jdbcType);
                return operations.execute((ConnectionCallback<Array>) c -> c.createArrayOf(typeName, value));
            }
        }
    }

Question

Would this (ugly) workaround be accepted as a PR or what would be the best way to get UUID support for arrays into Spring Boot JDBC?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 19, 2023
@mp911de mp911de self-assigned this Jul 19, 2023
@mp911de
Copy link
Member

mp911de commented Jul 19, 2023

JDBCType indeed does not contain UUID, however with our Dialect support we should be able to provide type hints for well-known types such as UUID.

@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 19, 2023
@mp911de mp911de changed the title Spring Data JDBC , Postgres and UUID array type not working together Add support for Postgres UUID arrays using JDBC Jul 19, 2023
mp911de added a commit that referenced this issue Jul 19, 2023
We now use Postgres' JDBC drivers TypeInfoCache to register and determine array types including support for UUID.

Closes #1567
mp911de added a commit that referenced this issue Jul 19, 2023
We now use Postgres' JDBC drivers TypeInfoCache to register and determine array types including support for UUID.

Closes #1567
@mp911de mp911de added this to the 3.2 M3 (2023.1.0) milestone Sep 6, 2023
mp911de added a commit that referenced this issue Sep 6, 2023
schauder pushed a commit that referenced this issue Sep 6, 2023
We now use Postgres' JDBC drivers TypeInfoCache to register and determine array types including support for UUID.

Closes #1567
schauder pushed a commit that referenced this issue Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants