从数据库获取数据时,JPA存储库出现错误,无法序列化 [英] Jpa Repository got error cannot not serializable when get data from database

查看:41
本文介绍了从数据库获取数据时,JPA存储库出现错误,无法序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用JPA存储库获取数据时遇到问题

每次尝试获取数据时,总是得到错误java.lang.ClassCastException:shurl.model.shurl不能强制转换为java.io.Serializable,

我已尝试探索解决方案,但到目前为止仍未找到解决此问题的任何线索

以下是我的错误:

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
)

有人能帮我吗?

推荐答案

我想我已经找到了没有可序列化的解决方案,我删除了Shurl类中的id属性,并将Shurl_Code设置为主键。

这篇关于从数据库获取数据时,JPA存储库出现错误,无法序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆