【发布时间】:2021-08-23 04:37:36
【问题描述】:
在我的 mysql 表中有存储“JSONArray”的列
这是 spring-boot 项目中模型类的一部分。
public class SubQuestions implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "sub_questionId", nullable = false,columnDefinition = "INT(11) UNSIGNED")
private Integer sub_questionId;
private JSONArray answers;
}
已经有模型类具有空构造函数、具有所有字段的构造函数、getter 和 setter。
这是我的 SubQuestionsRepository 接口。
public interface SubQuestionsRepository extends Serializable,JpaRepository<Questions,Integer>{
}
这是我的控制器类的一部分。
public class SubQuestionsController implements Serializable{
private SubQuestionsRepository subquestionsrepository;
public SubQuestionsController(SubQuestionsRepository subquestionsrepository) {
super();
this.SubQuestionsRepository = subquestionsrepository;
}
@GetMapping("/getall")
public Collection<SubQuestions> getallnestedques(){
return subquestionsrepository.getactiveques();
}
}
但是当我调用“getallnestedques()”方法时,它会出现以下错误。
There was an unexpected error (type=Internal Server Error, status=500).
could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
org.springframework.orm.jpa.JpaSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:353)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMeth
我该如何解决?
【问题讨论】:
-
SubQuestionsRepository应该扩展JpaRepository<SubQuestions,Integer>而不是JpaRepository<Questions,Integer>。不是吗?如果你在 Spring 中使用基于注解的配置,那么你应该分别使用@Entity、@Repository和@Controller来注解模型类、存储库和控制器。并在SubQuestionsModel 类中用 DB 中的相应列名标记answers字段。在提供答案时,将answers保存在数据库中的格式会更有帮助。
标签: java mysql spring-boot spring-data-jpa json-arrayagg