postgresql 使用Micronaut Data插入具有EmbeddedId的实体时多次指定错误列

5ssjco0h  于 2023-02-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(106)

我正在使用Micronaut Data JDBC遇到错误。我有此实体:

@MappedEntity(value = "document_metadata")
@AllArgsConstructor
@EqualsAndHashCode
public class DocumentMetadataJDBCEntity implements DocumentMetadata {

    @Embeddable
    @AllArgsConstructor
    public static class MetadataPk {

        @MappedProperty(value = "document_uid")
        @NotNull
        private UUID documentUid;

        @MappedProperty(value = "metadata_key")
        @NotNull
        private String metadataKey;

        public UUID getDocumentUid() {
            return documentUid;
        }

        public String getMetadataKey() {
            return metadataKey;
        }

    }

    @EmbeddedId
    private MetadataPk metadataPk;

    @NotNull
    private String metadataValue;

    public MetadataPk getMetadataPk() {
        return metadataPk;
    }

    @Override
    public String getMetadataKey() {
        return getMetadataPk().getMetadataKey();
    }

    @Override
    public String getMetadataValue() {
        return metadataValue;
    }

    public UUID getDocumentUid() {
        return getMetadataPk().getDocumentUid();
    }

}

插入时,我得到这个错误:

io.micronaut.data.exceptions.DataAccessException: SQL error executing INSERT: Batch entry 0 INSERT INTO "document_metadata" ("metadata_key","metadata_value","document_uid","document_uid","metadata_key") VALUES ('id','1234','c960d8de-99a4-40a6-91bf-b0d4a73910d6'::uuid,'c960d8de-99a4-40a6-91bf-b0d4a73910d6'::uuid,'id') was aborted: ERROR: column "document_uid" specified more than once

保存的代码是下一个:

Set<DocumentMetadataJDBCEntity> metadataSet = metadata.entrySet().stream()
                .map(e -> new DocumentMetadataJDBCEntity(new DocumentMetadataJDBCEntity.MetadataPk(
                        savedDocument.getUid(), e.getKey()), e.getValue())).collect(toSet());
        Iterable<DocumentMetadataJDBCEntity> persistedMetadata = documentMetadataJDBCRepository.saveAll(metadataSet);

你知道吗?

n1bvdmb6

n1bvdmb61#

@Transient添加到您的方便访问器(getter)方法中:

@Override
    @Transient
    public String getMetadataKey() {
        return getMetadataPk().getMetadataKey();
    }

    @Transient
    public UUID getDocumentUid() {
        return getMetadataPk().getDocumentUid();
    }

它“告诉”Micronaut不要将返回值保存到DB中。

相关问题