8

I have a problem when try to get data using JPA Repository,

every time i try to get data always get error java.lang.ClassCastException: shurl.model.Shurl cannot be cast to java.io.Serializable,

i have tried to explorer the solution, but until now still not found any clue to solve this problem

here my error : enter image description here

2019-04-03 07:36:17.434 ERROR 19348 --- [nio-5001-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: shurl.model.Shurl cannot be cast to java.io.Serializable] with root cause

java.lang.ClassCastException: shurl.model.Shurl cannot be cast to java.io.Serializable

and here my code at jpa repository

package shurl.repository

import shurl.model.Shurl
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Page
import java.time.LocalDate
import java.time.LocalDateTime
import shurl.model.ShurlHistory

@Repository
interface ShurlHistoryRepository : JpaRepository<ShurlHistory, String> {
    @Query("select b.* from shurl a " +
            "left join shurl_history b on b.shurl_code = a.shurl_code " +
            "where a.shurl_code = :code",nativeQuery = true )

    fun findShurlHistory(code:String):List<ShurlHistory>?
}

and here my model

package shurl.model

import com.fasterxml.jackson.annotation.JsonIgnore
import javax.persistence.*
import org.apache.poi.hpsf.Decimal
import java.time.LocalDate
import java.sql.Blob
import org.hibernate.type.BlobType
import java.time.LocalDateTime

@Entity
@Table(name = "shurl_history", schema = "dbo")
data class ShurlHistory (
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = true)
    var id:Long?=null,

    @Column(name = "create_date", nullable = false )
    var createDate:LocalDateTime = LocalDateTime.now(),


    @ManyToOne
    @JoinColumn(name = "shurl_code", referencedColumnName = "shurl_code", nullable = true)
    var shurl : Shurl? = null
)

can someone help me?

Hansen
  • 650
  • 1
  • 11
  • 32
  • Read the error message: your `Shurl` class needs to implement `Serializable`. – gidds Apr 03 '19 at 07:21
  • 1
    i think i have found the solution without serializable, i remove id properties in class shurl and i set shurl_code as primary key.. thank for respon – Hansen Apr 04 '19 at 02:47

2 Answers2

32

If you reference another object using non-primary key column, make the referenced object serializable.

It's a known bug reported in 2012 and still unsolved.

Make Shurl serialzable and it should work:

@Entity
public class Shurl implements Serializable {

    private static final long serialVersionUID = 1L;
}
CDT
  • 10,165
  • 18
  • 66
  • 97
0

i think i have found the solution without serializable, i remove id properties in class shurl and i set shurl_code as primary key..

Hansen
  • 650
  • 1
  • 11
  • 32