英文:
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<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
}
}
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 = "",
var domain: String = ""
)
答案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<Record, Long>{
@Query("SELECT DISTINCT million.parentDomain FROM Record million")
fun findByDomain(domain: String): List<String>
}
答案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("SELECT DISTINCT million.parentDomain FROM Record million where milion.domain = :domain")
fun findByDomain(@Param("domain") domain: String): List<String>
答案3
得分: 0
将“List Record”替换为接口RecordRepository中的List。
英文:
Replace "List Record " with List in interface RecordRepository
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论