【问题标题】:Spring JPA: BaseEntity is not mappedSpring JPA:BaseEntity 未映射
【发布时间】:2020-07-25 10:25:03
【问题描述】:

我正在与JPA Persistence 合作开展Spring Boot 项目。
我有一个基本实体类,例如:

@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
@Access(AccessType.FIELD)
abstract class BaseEntity<T: Serializable> : Persistable<T> {

    @Id @GeneratedValue
    private var id: T? = null
    // ...
}

实体类如:

@Entity
@Where(clause = "deleted = false")
class User(
    @Column
    val username: String,
    @Column
    val password: String,
    //...
) : BaseEntity<Long>()

我不想从数据库中物理删除记录,所以我添加了一个 BaseRepository 类,并重写了 deleteById() 方法,如下所示:

interface BaseRepository<T : BaseEntity<Long>?, ID> : JpaRepository<T, ID>, JpaSpecificationExecutor<T> {

    @Modifying
    @Query("update #{#entityName} o set o.deleted = true where o.id = ?1")
    override fun deleteById(@NonNull id: ID)
} 

现在运行项目后出现此错误:

Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'baseRepository': Invocation of init method failed; 
nested exception is java.lang.IllegalArgumentException: 
Validation failed for query for method
public abstract void com.example.repo.BaseRepository.deleteById(java.lang.Object)!

有什么想法吗?

【问题讨论】:

    标签: spring-boot kotlin spring-data-jpa


    【解决方案1】:

    我找到了解决方案。使用 @NoRepositoryBean 注释对 BaseRepository 进行注释。这确保 Spring Data JPA 不会尝试为 BaseRepository 接口创建实现:

    @NoRepositoryBean 
    interface BaseRepository<T : BaseEntity<Long>?, ID> : JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
         // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 2017-12-24
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 2016-03-03
      • 2014-03-30
      相关资源
      最近更新 更多