从数据库获取数据时,无法序列化Jpa Repository got错误无法序列化

时间:2019-04-03 01:12:35

标签: spring-boot jpa kotlin repository

尝试使用JPA存储库获取数据时出现问题,

每次我尝试获取数据时,总会出现错误java.lang.ClassCastException:shurl.model.Shurl无法转换为java.io.Serializable,

我曾尝试探索解决方案,但直到现在仍未找到解决此问题的任何线索

这是我的错误: 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

这里是我在jpa存储库中的代码

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>?
}

这里是我的模型

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
)

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

我认为我找到了无法序列化的解决方案,我在shurl类中删除了id属性,并将shurl_code设置为主键。

答案 1 :(得分:0)

如果您使用非主键列引用另一个对象,则使引用的对象可序列化。

这是一个known bug,创建于2012年,至今仍未解决。

使Shurl可序列化,它应该可以工作:

@Entity
public class Shurl implements Serializable {

    private static final long serialVersionUID = 1L;
}