带有 JDBC + JPA 注释的 Micronaut 数据 findById 不返回实体对象

问题描述 投票:0回答:1

使用 Micronaut 数据配合 JDBC + JPA 注解,获取具有以下依赖关系的实体数据

dependencies {
    annotationProcessor("io.micronaut.data:micronaut-data-processor")
    // Others

    implementation("io.micronaut.data:micronaut-data-jdbc")

    compileOnly("jakarta.persistence:jakarta.persistence-api")
    // Others

}

存储库

@JdbcRepository(dialect = Dialect.POSTGRES)
public interface IJobRepository extends CrudRepository<Job, UUID>, JpaSpecificationExecutor<Job> {
    Optional<Job> findById(UUID id);
    Optional<Job> findByTitle(String title);
}

在服务中

@Singleton
public non-sealed class JobService implements IJobService {

    @Override
    public Result<JobResponse> get(@Nonnull UUID id) {
        return iJobRepository.findById(id)
                .map(x -> Result.of(jobResponseMapper.apply(x)))
                .orElseGet(() -> Result.of(new Exception(ApplicationMessage.TagMessage.TAG_NOTFOUND)));
    }
}

工作实体

@AllArgsConstructor
@Builder
@Setter
@Getter
@Entity
public final class Job extends AuditTrail {

    @NotNull
    private String title;

    @TypeDef(type = DataType.JSON)
    @Column(columnDefinition = "jsonb")
    @Nullable
    private List<UUID> tag = new ArrayList<>();

    @Column(columnDefinition = "TEXT")
    private String description;

    @Column(columnDefinition = "TIMESTAMP WITH TIME ZONE")
    @NotNull
    private Instant startDate;

    @Column(columnDefinition = "TIMESTAMP WITH TIME ZONE")
    @NotNull
    private Instant endDate;

    @Enumerated(EnumType.STRING)
    @NotNull
    private JobType type;

    @Enumerated(EnumType.STRING)
    @Column(length = 100)
    @NotNull
    private TeachingLevel teachingLevel;

    private String externalLink;

    @Enumerated(EnumType.STRING)
    @Column(length = 100)
    @NotNull
    private ResidenceStatus residenceStatus;

    @Enumerated(EnumType.STRING)
    @Column(length = 100)
    @NotNull
    private Experience experience;

    private String subjectToTeach;

    @TypeDef(type = DataType.JSON)
    @Column(columnDefinition = "jsonb")
    @Nullable
    private List<UUID> perk;

    @Enumerated(EnumType.STRING)
    @Column(length = 100)
    @NotNull
    private JobStatus status;

    public static final List<SortOption> SUPPORTED_SORTS = List.of(
            SortOption.of("title", SortOperators.ASC)
    );

    public static final List<FilterOption> SUPPORTED_FILTERS = List.of(
            FilterOption.of("title", FilterOperators.EQUAL, Collections.emptyMap()),
            FilterOption.of("startDate", FilterOperators.IN, Collections.emptyMap())
    );

    public Job() {
        super();
    }
}

AuditTrail 实体

@MappedSuperclass
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditTrailEventListener.class)
public sealed class AuditTrail extends EntityId
        permits Job {
    private UUID createdBy;

    @Column(columnDefinition = "TIMESTAMP WITH TIME ZONE")
    private Instant createdDate;

    private UUID lastModifiedBy;

    @Column(columnDefinition = "TIMESTAMP WITH TIME ZONE")
    private Instant lastModifiedDate;
}

EntityId 实体

@NoArgsConstructor
@Getter
@Setter
@AllArgsConstructor
@MappedSuperclass
public sealed class EntityId permits AuditTrail {
    @Id
    @AutoPopulated
    private UUID id;
}

findById
方法返回的对象值

enter image description here

正如我们在屏幕截图中看到的,作业

entity
中的所有属性都没有映射。我确实在工作实体中尝试过
@Introspected
,但是同样的问题

micronaut micronaut-data micronaut-rest micronaut-serde
1个回答
0
投票

不确定问题是什么,但编写下面的代码,映射实体

public Result<JobResponse> get(@Nonnull UUID id) {
    var job = iJobRepository.findById(id).orElse(null);
    if (job == null)
        Result.of(new Exception(ApplicationMessage.TagMessage.TAG_NOTFOUND));
    return Result.of(jobResponseMapper.apply(job));
}
© www.soinside.com 2019 - 2024. All rights reserved.