Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jpa Repository got error cannot not serializable when get data from database

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?

like image 780
Hans Avatar asked Apr 03 '19 01:04

Hans


1 Answers

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;
}
like image 185
CDT Avatar answered Oct 24 '22 00:10

CDT