如何使用 Spring 域示例 + Spring 可审计域

How to use Spring Domain Example + Spring Domain Auditable

我将 Spring Data Domain Example 与实现 Spring Data Domain Auditable 的实体一起使用时遇到以下问题。

错误: org.springframework.dao.InvalidDataAccessApiUsageException:参数值 [Optional.empty] 与预期类型 [java.time.LocalDateTime (n/a)] 不匹配;嵌套异常是 java.lang.IllegalArgumentException: 参数值 [Optional.empty] 与预期类型不匹配 [java.time.LocalDateTime (n/a)]

实体:

import org.springframework.data.domain.Auditable;

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "my_table")
@Builder
public class MyEntity implements Serializable, Auditable<String, Integer, LocalDateTime>

    @Getter
    @Setter
    @Id
    @Column(name = "my_table_id", nullable = false)
    private Integer id;

    // Some fields
    // ...

    // AUDIT
    @Column(name = "my_table_created_by")
    @CreatedBy
    private String createdBy;
    
    @Column(name = "my_table_created_date")
    @CreatedDate
    @Type(type = "org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTime")
    private LocalDateTime createdDate;
    
    @Column(name = "my_table_last_modified_by")
    @LastModifiedBy
    private String lastModifiedBy;
    
    @Column(name = "my_table_last_modified_date")
    @LastModifiedDate
    @Type(type = "org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTime")
    private LocalDateTime lastModifiedDate;
    
    @Override
    public boolean isNew()
    {
        return id == null;
    }
    
    @NotNull
    @Override
    public Optional<String> getCreatedBy()
    {
        return Optional.ofNullable(createdBy);
    }
    
    @Override
    public void setCreatedBy(@NotNull String createdBy)
    {
        this.createdBy = createdBy;
    }
    
    @NotNull
    @Override
    public Optional<LocalDateTime> getCreatedDate()
    {
        return Optional.ofNullable(createdDate);
    }
    
    @Override
    public void setCreatedDate(
            @NotNull
                    LocalDateTime creationDate)
    {
        this.createdDate = creationDate;
    }
    
    @NotNull
    @Override
    public Optional<String> getLastModifiedBy()
    {
        return Optional.ofNullable(lastModifiedBy);
    }
    
    @Override
    public void setLastModifiedBy(@NotNull String lastModifiedBy)
    {
        this.lastModifiedBy = lastModifiedBy;
    }
    
    @NotNull
    @Override
    public Optional<LocalDateTime> getLastModifiedDate()
    {
        return Optional.ofNullable(lastModifiedDate);
    }
    
    @Override
    public void setLastModifiedDate(@NotNull LocalDateTime lastModifiedDate)
    {
        this.lastModifiedDate = lastModifiedDate;
    }

}

测试:

import org.springframework.data.domain.Example;

@Test
public void test()
{
    MyEntity entity = MyEntity.builder()
         .someField("someValue")
         .build();

    List<MyEntity> entities = repository.findAll(Example.of(entity));
}

我想这与我在 Auditable 上的 Optional Methods 有关,但我不知道如何解决这个问题。
如果我在没有 Auditable 的情况下复制实体 class,它 有效 但它 不干净 .

修复,我不需要实施 Auditable 因为我已经使用 @EntityListeners(AuditingEntityListener.class).