• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Cast error for Spring Data JPA customized interface

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody. I need help. After retrieving data from database and access to object field i have exception.

-------------  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)

// !!! after this
val test = records?.first()?.timestamp
/*
!!!  error:
[Ljava.lang.Object; cannot be cast to com.blabla.hit.Record
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.blabla.hit.Record

*/
}
}

Why exception happen?
 
Bartender
Posts: 2061
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not familiar with Kotlin.
But are you sure what you put in the record repository is a Record object type , not an Object type ?
 
Serg Speerin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:I am not familiar with Kotlin.
But are you sure what you put in the record repository is a Record object type , not an Object type ?


Hi !  Do you mean how I save the records to the database?
for (i in 0..10) {
         rec = Record().apply {
               description = "week $weekNum event activity $i"
               lengthInMSec = MIN_TIME_STEP * (i + 2) / 2
               timestamp = Timestamp(currTimestamp)
               currTimestamp += (lengthInMSec + MIN_TIME_STEP)
         }
         val test = recordRepository?.save(rec)
         test
}
 
Himai Minh
Bartender
Posts: 2061
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, when you retrieve the Record from the repository, make sure that Record is in the right type.
Maybe, in Kotlin, all records retrieved from repository is an object?
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic