spring - Spring Data JPA 自定义接口的转换错误
问题描述
我需要帮助。从数据库中检索数据并访问对象字段后,我遇到了异常。
------------- build.gradle.kts --------------
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.4.3-SNAPSHOT"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
id("com.vaadin") version "0.14.3.7"
kotlin("jvm") version "1.4.21"
kotlin("plugin.spring") version "1.4.21"
kotlin("plugin.jpa") version "1.4.21"
}
group = "com.meetroom"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
}
extra["vaadinVersion"] = "14.4.7"
dependencies {
implementation("org.postgresql:postgresql")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-mustache")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-web-services")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("com.vaadin:vaadin-spring-boot-starter")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
implementation("org.thymeleaf.extras:thymeleaf-extras-springsecurity5")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.springframework.security:spring-security-test")
}
dependencyManagement {
imports {
mavenBom("com.vaadin:vaadin-bom:${property("vaadinVersion")}")
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
//------------------------------ Record.kt ------------------------------------
package com.blabla.hit
import java.io.Serializable
import javax.persistence.*
@Entity
@Table(name = "record")
class Record : Serializable {
@javax.persistence.Id
@GeneratedValue(strategy = GenerationType.AUTO)
var id = 0
@Column(name = "description")
var description : String? = null
@Column(name = "timestamp")
var timestamp: java.sql.Timestamp? = null
}
//--------------- Repository.kt ----------------------------
package com.blabla.hit
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
import java.sql.Timestamp
@Repository
public interface RecordRepository: CrudRepository<Record, Long>, CustomizedRecords<Record>
interface CustomizedRecords<T> {
@Query("select id,description,timestamp,length from Record where timestamp >= :from and timestamp <= :to order by timestamp")
fun getRange(from: Timestamp, to: Timestamp): List<T>
}
fun getWeekRecords(repo: RecordRepository, shiftOfWeek: Int): List<Record>{
return try {
val from = Timestamp(getBeginTsOfWeek(shiftOfWeek).timeInMillis)
val to = Timestamp(getEndTsOfWeek(shiftOfWeek).timeInMillis)
val res = repo.getRange(from, to)
res
}
catch (e: Exception){
listOf<Record>()
}
}
// ----------------------------------------
@EntityScan
@SpringBootTest
class HemmulApplicationTests {
@Autowired
private val repo: RecordRepository? = null
@Test
fun contextLoads() {
val records = repo.getRange(from, to)
val test = records?.first()?.timestamp
}
}
// !!! 在这之后 :
val test = records?.first()?.timestamp
!!!错误:[Ljava.lang.Object; 无法转换为 com.blabla.hit.Record java.lang.ClassCastException: [Ljava.lang.Object; 不能转换为 com.blabla.hit.Record
从数据库中检索数据并访问对象字段后,我遇到了异常。为什么会发生异常?
解决方案
推荐阅读
- azure - Terraform (azurerm):使用同一资源组创建多个静态 IP?
- javascript - MongoDB聚合:如何返回具有与数组中的值匹配的属性的文档?
- sockets - C中的Socket编程,需要创建一个可以与两个端口通信的套接字,一个是安全的,另一个是不安全的
- node.js - AWS Codebuild 中的测试用例
- python - 了解 PATH 相对于 Conda 环境
- scope - 在范围内找不到变量
- gcc - 为什么我在 C++ 中出现此错误,请帮助我解决此问题
- rust - 当我在 RefCell<> 上内联借用时,Rust 出现恐慌
- node.js - 将 SSL 证书连接到前端后,请求不会发送到后端
- bluetooth-lowenergy - hcitool 找不到 ble 设备,但 blueman 找到了,bettercap 和 bluetoothctl 也找到了