spring数据jpa自定义接口的强制转换错误

vltsax25  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(250)

我需要帮助。从数据库检索数据并访问对象字段之后,我遇到了一个异常。

-------------  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=记录?.first()?.timestamp
!!! 错误:[ljava.lang.object;无法转换为com.blabla.hit.record java.lang.classcastexception:[ljava.lang.object;无法转换为com.blabla.hit.record
从数据库检索数据并访问对象字段之后,我遇到了一个异常。为什么会发生异常?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题