Jackie Tian
Jackie Tian

Reputation: 31

Kotlin use SpringData Jpa custom repository

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 code is:

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
    }

}

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?

Upvotes: 1

Views: 3260

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 82008

Rename ChapterDaoImpl to ChapterRepositoryImpl.

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

You named the implementation based on the custom interface.

Upvotes: 4

Related Questions