【问题标题】:How to use Spring Domain Example + Spring Domain Auditable如何使用 Spring Domain 示例 + Spring Domain Auditable
【发布时间】:2021-12-01 18:34:33
【问题描述】:

我在使用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));
}

我想这与 Optional MethodsAuditable 有关,但我不知道如何解决这个问题。
如果我复制没有Auditable 的实体类,它有效,但它不干净

【问题讨论】:

    标签: java spring spring-data-jpa spring-data


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-03-26
      • 1970-01-01
      • 2019-07-02
      • 2017-03-03
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 2015-04-23
      相关资源
      最近更新 更多