gradle org.springframework.orm.jpa.JpaSystemException:无法反序列化

yyhrrdl8  于 2023-02-23  发布在  Spring
关注(0)|答案(2)|浏览(120)

我想创建mysql字符串数组类型的数据字段,因此我在spring-boot gradle项目中创建了模型类,如下所示。

@Entity
    @Table(name = "single_questions")
    public class SingleQuestions {
        @Id
        private Integer id;
        private String[] fieldtypevalues;
    
        public SingleQuestions() {
        }
    
        public SingleQuestions(Integer id, String category, String field, String field_type, String[] fieldtypevalues) {
            this.id = id;
            this.fieldtypevalues = fieldtypevalues;
        }
    
        public Integer getId() {
            return this.id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
    
        public String[] getFieldtypevalues() {
            return this.fieldtypevalues;
        }
    
        public void setFieldtypevalues(String[] fieldtypevalues) {
            this.fieldtypevalues = fieldtypevalues;
        }
    
    }

这是我的存储库,它扩展了JpaRepository

public interface SingleQuestionRepository extends JpaRepository<SingleQuestions,Integer>{
        
    }

这是控制器类

@RestController
    @RequestMapping("/singleques")
    @CrossOrigin(origins = "*")
    public class SingleQuestionController {
        private SingleQuestionRepository singleQuestionRepository;
    
        public SingleQuestionController(SingleQuestionRepository singleQuestionRepository) {
            this.singleQuestionRepository = singleQuestionRepository;
        }
    
        @GetMapping("/getall")
        Collection<SingleQuestions> getquestions() {
            return singleQuestionRepository.findAll();
        }
    }

但是当我在控制器类中运行getquestions()函数时,它给出了以下错误。

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)
ttygqcqt

ttygqcqt1#

尝试使用@ElementCollection注解String[]字段。

@ElementCollection(targetClass=String.class)
@Column(name = "field_type_values")
private String[] fieldTypeValues;
tct7dpnv

tct7dpnv2#

我不知道您是否需要允许重复的值,但如果不是这种情况,使用java.util.Set type作为集合类型,沿着使用@ElementCollection,对我来说是有效的。

@Entity
    @Table(name = "single_questions")
    public class SingleQuestions {
        @Id
        private Integer id;

        @ElementCollection(targetClass=String.class)
        @Column(name = "FIELD_TYPE_VALUES")
        private List<String> fieldtypevalues;

相关问题