Modelmapper is not recognizing UUIDs

橙三吉。 提交于 2020-12-15 03:47:47

问题


Hey I just began playing around with Modelmapper to map jOOQ records to POJOs.

This is the schema for the table whose records I am attempting to convert (Postgresql)

CREATE TABLE IF NOT EXISTS actor(
    actor_id UUID DEFAULT uuid_generate_v4(),
    first_name VARCHAR(256) NOT NULL,
    last_name VARCHAR(256) NOT NULL,
    PRIMARY KEY(actor_id)
);

Here is what the POJO looks like:

@JsonDeserialize(builder = Actor.Builder.class)
public class Actor {
    private final UUID actorId;
    private final String firstName;
    private final String lastName;

    private Actor(final Builder builder) {
        actorId = builder.actorId;
        firstName = builder.firstName;
        lastName = builder.lastName;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public UUID getActorId() {
        return actorId;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static final class Builder {
        private UUID actorId;
        private String firstName;
        private String lastName;

        private Builder() {
        }

        public Builder withActorId(final UUID val) {
            actorId = val;
            return this;
        }

        public Builder withFirstName(final String val) {
            firstName = val;
            return this;
        }

        public Builder withLastName(final String val) {
            lastName = val;
            return this;
        }

        public Actor build() {
            return new Actor(this);
        }
    }
}

I am creating a ModelMapper bean in my application and registering a UUID converter to it.

   @Bean
    public ModelMapper modelMapper() {
        final ModelMapper mapper = new ModelMapper();
        Provider<UUID> uuidProvider = new AbstractProvider<UUID>() {
            @Override
            public UUID get() {
                return UUID.randomUUID();
            }
        };

        final Converter<String, UUID> uuidConverter = new AbstractConverter<>() {
            @Override
            protected UUID convert(final String source) {
                return UUID.fromString(source);
            }
        };
        mapper.createTypeMap(String.class, UUID.class);
        mapper.addConverter(uuidConverter);
        mapper.getTypeMap(String.class, UUID.class).setProvider(uuidProvider);


        mapper.getConfiguration()
                .setSourceNameTokenizer(NameTokenizers.UNDERSCORE)
                .addValueReader(new RecordValueReader())
                .setDestinationNameTransformer(NameTransformers.builder("with"))
                .setDestinationNamingConvention(NamingConventions.builder("with"));
        mapper.validate();
        return mapper;
    }

I then use the model mapper to map the ActorRecord from the jOOQ autogenerated code to the POJO

public Optional<Actor> getActor(final UUID actorId) {
    return Optional.ofNullable(dsl.selectFrom(ACTOR)
            .where(ACTOR.ACTOR_ID.eq(actorId))
            .fetchOne())
            .map(e -> modelMapper.map(e, Actor.Builder.class).build());
}

This works except the UUID is always null. For example:

{"actor_id":null,"first_name":"John","last_name":"Doe"}

However when I change the following in the Builder:

    public Builder withActorId(final String val) {
        actorId = UUID.fromString(val);
        return this;
    }

It works! Unfortunately this does not work with an overloaded method:

    public Builder withActorId(final String val) {
        actorId = UUID.fromString(val);
        return this;
    }

    public Builder withActorId(final UUID val) {
        actorId = val;
        return this;
    }

As this also returns null.

You can see from the autogenerated jOOQ code it should be handling a UUID:

/**
 * The column <code>public.actor.actor_id</code>.
 */
public final TableField<ActorRecord, UUID> ACTOR_ID = createField(DSL.name("actor_id"), org.jooq.impl.SQLDataType.UUID.nullable(false).defaultValue(org.jooq.impl.DSL.field("uuid_generate_v4()", org.jooq.impl.SQLDataType.UUID)), this, "");

I am not sure what I am exactly missing. I do not want to create a custom converter for each of my entities as I have a lot of them and they all contain (at least 1) UUID. Ideally I want to configure the ModelMapper to know about UUID and whenever it sees one it can handle it. Thanks!

NOTE: I also tried this with Lombok @Data object and it does not work either.

@JsonDeserialize(builder = Actor.ActorBuilder.class)
@Data
public class Actor {
    private UUID actorId;
    private String firstName;
    private String lastName;

    @JsonPOJOBuilder(withPrefix = "with")
    public static class ActorBuilder {

    }
}

回答1:


UUID.fromString(val) is not allowed. I had same problem yesterday. Try to put to ModelMapper configurations UUID converted to String.



来源:https://stackoverflow.com/questions/64542309/modelmapper-is-not-recognizing-uuids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!