0

我目前正在使用带有 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 也无济于事。

4

1 回答 1

2

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

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

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

于 2018-01-11T08:33:48.463 回答