jpa - Kotlin 使用 SpringData Jpa 自定义仓库

标签 jpa repository spring-data kotlin

这是我的代码。我自定义了我的存储库。

interface ChapterDao {
    fun test(novelId:String):List<Chapter>
}

class ChapterDaoImpl constructor(@PersistenceContext var entityManager: EntityManager){   

    fun test(novelId: String): List<Chapter> {
        val query = entityManager.createNativeQuery("select c.name, c.number from chapter c where c.novel.id = $novelId")
        val resultList = query.resultList as Array<Array<Any>>
        var chapterList:ArrayList<Chapter> = ArrayList<Chapter>()
        for (item in resultList){
            chapterList.add(Chapter(item.get(0) as String,item.get(1) as Int))
        }
        return chapterList
    }
}

interface ChapterRepository : CrudRepository<Chapter, String>, ChapterDao {

}
Chapter代码是:
package com.em248.entity;

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonInclude
import java.util.*
import javax.persistence.*

@Entity
@Table(name = "chapter")
@com.fasterxml.jackson.annotation.JsonInclude(JsonInclude.Include.NON_EMPTY)
class Chapter {
    @Id
    var id: String = UUID.randomUUID().toString()

    var number: Int = -1

    var name: String = ""

    @Column(columnDefinition = "text")
    var content: String? = ""

    @Temporal(TemporalType.TIMESTAMP)
    var createDate: Date = Date()

    @ManyToOne
    @JoinColumn(name = "novel_id")
    @JsonIgnore
    var novel: Novel = Novel();

    constructor()
    constructor(name: String, number: Int)
    constructor(number: Int, name: String, content: String?, createDate: Date, novel: Novel) {
        this.number = number
        this.name = name
        if (content != null) this.content = content
        this.createDate = createDate
        this.novel = novel
    }

}

但是当使用 test函数,它会抛出一个错误:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property test found for type Chapter!
    at org.springframework.data.mapping.PropertyPath.lambda$new$0(PropertyPath.java:82) ~[spring-data-commons-2.0.0.M3.jar:na]
    at java.util.Optional.orElseThrow(Optional.java:290) ~[na:1.8.0_111]
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:82) ~[spring-data-commons-2.0.0.M3.jar:na]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:304) ~[spring-data-commons-2.0.0.M3.jar:na]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:284) ~[spring-data-commons-2.0.0.M3.jar:na]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-2.0.0.M3.jar:na]

我搜索如何实现自定义存储库,但我没有看到我的代码有什么不同?

最佳答案

重命名 ChapterDaoImplChapterRepositoryImpl .

Spring Data 查找以 Repository Interface + Impl 命名的自定义实现.

您根据自定义接口(interface)命名了实现。

关于jpa - Kotlin 使用 SpringData Jpa 自定义仓库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44081521/

相关文章:

c# - 如何在 C# 中抽象实体数据上下文

java - 当前 CallableStatement ou 不是 ResultSet,但调用了 getResultList (MySQL)

Spring-boot JPA连接到运行时提供数据库和模式的postgres

java - JPA - 抽象基实体类中的多对多关系和具体扩展实体子类中的连接表引用

java - 当我们使用合并时,JPA 不会从一对多关系中插入新的 child

mysql - SQL 到 JPQL 的左连接为空

java - 映射属性具有相同类型的 jpa 关系

git - 这些线有什么作用?

svn - 与多个用户一起使用 SVN

java - Spring Data 的 Pageable 是否采用页码或查询偏移量?