Spring Data JPA,在我尝试选择DistinctValue时出现错误。

huangapple go评论84阅读模式
英文:

Spring Data JPA, error when I'm trying to select DistinctValue

问题

以下是翻译好的部分:

我在尝试使用 Spring Data JPA 从数据库中选择不同的值时遇到了这个错误。

无法从类型 [java.util.ArrayList] 转换为类型
[@org.springframework.data.jpa.repository.Query
java.util.List<com.example.million.model.Record>],对于值 '[a, b, c]' 的嵌套异常是
org.springframework.core.convert.ConverterNotFoundException: 找不到能够从类型 [java.lang.String] 转换为类型 [@org.springframework.data.jpa.repository.Query
com.example.million.model.Record] 的转换器。

interface RecordRepository: CrudRepository<Record, Long>{

    @Query("SELECT DISTINCT million.parentDomain FROM Record million")
    fun findByDomain(domain: String): List<Record>
}

@Service
class RecordService(val recordRepository: RecordRepository) {

    fun fetchByDomain(domain: String): List<Record> {

        val list = arrayListOf<Record>()

        println(recordRepository.findByDomain(domain))
        recordRepository.findByDomain(domain).forEach { list.add(it) }

        return list
    }
}

我有一个名为 "million" 的数据库和其中的一个名为 "record" 的表(其中一个字段是 "parent_domain")。
更新:

@Entity
data class Record(
        @Id @GeneratedValue val id: Long? = null,
        var parentDomain: String = "",
        var domain: String = ""
)
英文:

I'm getting this error while trying to select distinct value from database using Spring Data JPA.

> Failed to convert from type [java.util.ArrayList<?>] to type
> [@org.springframework.data.jpa.repository.Query
> java.util.List<com.example.million.model.Record>] for value '[a, b,
> c]'; nested exception is
> org.springframework.core.convert.ConverterNotFoundException: No
> converter found capable of converting from type [java.lang.String] to
> type [@org.springframework.data.jpa.repository.Query
> com.example.million.model.Record]

interface RecordRepository: CrudRepository&lt;Record, Long&gt;{

    @Query(&quot;SELECT DISTINCT million.parentDomain FROM Record million&quot;)
    fun findByDomain(domain: String): List&lt;Record&gt;
}

    @Service
class RecordService(val recordRepository: RecordRepository) {

    fun fetchByDomain(domain: String): List&lt;Record&gt; {

        val list = arrayListOf&lt;Record&gt;()

        println(recordRepository.findByDomain(domain))
        recordRepository.findByDomain(domain).forEach { list.add(it) }

        return list
    }
}

I have db "million" and table record inside (and one of the field is parent_domain)
Update:

@Entity
data class Record(
        @Id @GeneratedValue val id: Long? = null,
        var parentDomain: String = &quot;&quot;,
        var domain: String = &quot;&quot;
)

答案1

得分: 1

我认为你正在尝试进行投影,而这个视图无法映射到 Record,如果你只关心 parentDomain,请尝试进行如下操作:

interface RecordRepository: CrudRepository<Record, Long>{

    @Query("SELECT DISTINCT million.parentDomain FROM Record million")
    fun findByDomain(domain: String): List<String>;
}
英文:

I think you are trying to make a projection and this view can not be mapped to a Record try to to make this if you care about the parentDomain only

interface RecordRepository: CrudRepository&lt;Record, Long&gt;{

    @Query(&quot;SELECT DISTINCT million.parentDomain FROM Record million&quot;)
    fun findByDomain(domain: String): List&lt;String&gt;
}

答案2

得分: 0

为了获取唯一的 parentDomain,您应该使用以下查询:

@Query("SELECT DISTINCT million.parentDomain FROM Record million where million.domain = :domain")
fun findByDomain(@Param("domain") domain: String): List<String>
英文:

In order to get unique parentDomain you should use following query:

@Query(&quot;SELECT DISTINCT million.parentDomain FROM Record million where milion.domain = :domain&quot;)
fun findByDomain(@Param(&quot;domain&quot;) domain: String): List&lt;String&gt;

答案3

得分: 0

将“List Record”替换为接口RecordRepository中的List。

英文:

Replace "List Record " with List in interface RecordRepository

huangapple
  • 本文由 发表于 2020年4月7日 17:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61076411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定