【问题标题】:Kotlin and Spring Data JPA produces PropertyReferenceExceptionKotlin 和 Spring Data JPA 产生 PropertyReferenceException
【发布时间】:2023-03-13 06:02:01
【问题描述】:

我目前正在使用带有 Spring Data JPA 的 Kotlin 1.2 和 Spring Boot 2.0 M7。在这个项目中,我使用的是自定义基础存储库,而不是 JPARepository 或 PagingAndSortingRepository(真的没关系)

这是基本界面

@NoRepositoryBean
interface BaseRepository<T, ID : Serializable> : Repository<T, ID> {
    fun <S : T> save(entity: S): S
    fun findOne(id: ID): T?
    fun findAll(): List<T>
    fun count(): Long
}

这是实际的存储库

interface ArticleRepository : BaseRepository<Article, Int> {
}

最后是文章数据类

@Entity
@Table(name = "article")
@Cacheable
data class Article (
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    var id: Int? = null,

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "member_id", nullable = false)
    var member: Member? = null,

    @Column(name = "title", nullable = false, length = 200)
    var title: String = "",

    @Column(name = "content", nullable = false, length = 65535)
    var content: String = "",

    @Column(name = "last_modified", nullable = false, length = 19)
    var lastModified: LocalDateTime = LocalDateTime.now(),

    @Column(name = "deleted", nullable = false)
    var deleted: Boolean = false,

    @Column(name = "effective_start", length = 19)
    var effectiveStart: LocalDateTime? = null,

    @Column(name = "effective_end", length = 19)
    var effectiveEnd: LocalDateTime? = null,

    @Version
    @Column(name = "version", nullable = false)
    var version: Int = 0
): Serializable {
    constructor() : this(null)
    constructor(member: Member, title: String, content: String, lastModified: LocalDateTime, deleted: Boolean) : this(null, member, title, content, lastModified, deleted)
}

但是在启动时,我得到了这个奇怪的错误

Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Object com.nokia.srandu.oms.corrviewer.db.repo.BaseRepository.findOne(java.io.Serializable)! No property findOne found for type Article!
.
.
.
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findOne found for type Article!

我认为这与https://jira.spring.io/browse/DATACMNS-1223 有关,但我能做些什么来解决这个问题?将接口转移到 Java 也无济于事。

【问题讨论】:

  • 为什么要创建 BaseRepository?您可以使用 Spring Data 中的 JPARepository。
  • @SimonMartinelli 实际上,如果我使用 JPARepository,它仍然是相同的错误,但方法不同。我没有使用 JPARepository,因为我不想暴露物理删除方法

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


【解决方案1】:

存储库的方法名称约定在 Spring Data 2.0 中发生了变化:

fun findOne(…): T? -> fun findById(…): Optional&lt;T&gt;

如果您想继续获得一个简单的可为空类型而不是 Optional,请声明一个附加或备用查询方法 fun getById(…): T?,它应该可以按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-01
    • 2020-01-27
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 2013-06-06
    • 2017-11-29
    相关资源
    最近更新 更多