Kotlin和Spring Data JPA产生PropertyReferenceException

时间:2018-01-11 06:29:58

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

我目前正在使用带有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!

我认为这与this one有关,但我可以做些什么来解决这个问题呢?将接口转移到Java也没有帮助。

1 个答案:

答案 0 :(得分:2)

存储库的方法名称约定在Spring Data 2.0中已更改:

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

如果您希望继续获取简单的可空类型而不是Optional,请声明其他或备用查询方法fun getById(…): T?,它应该按预期工作。