Kotlin使用SpringData Jpa自定义存储库 [英] Kotlin use SpringData Jpa custom repository

查看:74
本文介绍了Kotlin使用SpringData Jpa自定义存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my code. I customized my repository.

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函数时,它会引发错误:

But when using the test function, it throws an error:

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]

我搜索了如何实现自定义存储库,但是我看不出代码的不同之处?

I search how to implement a custom repository, but I don't see the difference to my code?

推荐答案

ChapterDaoImpl重命名为ChapterRepositoryImpl.

Spring Data查找以存储库接口+ Impl命名的自定义实现.

Spring Data looks for custom implementations named after the Repository Interface + Impl.

您基于自定义界面对实现进行了命名.

You named the implementation based on the custom interface.

这篇关于Kotlin使用SpringData Jpa自定义存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆