【发布时间】:2019-08-24 10:04:15
【问题描述】:
我在尝试使用 JPA 存储库获取数据时遇到问题,
每次我尝试获取数据时总是得到错误 java.lang.ClassCastException: shurl.model.Shurl cannot be cast to 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类需要实现Serializable。 -
我想我找到了没有可序列化的解决方案,我删除了 shurl 类中的 id 属性并将 shurl_code 设置为主键.. 感谢回复
标签: spring-boot jpa kotlin repository