微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Spring boot JPA 从 mysql 数据库获取 JSONArray

如何解决使用 Spring boot JPA 从 mysql 数据库获取 JSONArray

在我的 MysqL 表中有列存储 'JSONArray'

这是 spring-boot 项目中模型类的一部分。

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'findindex' of undefined
    at User.addToCart (D:\Pradip_All\Nodejs- tutorials video (MONGODB)\models\user.js:20:46)
    at D:\Pradip_All\Nodejs- tutorials video (MONGODB)\controllers\shop.js:70:23
    at processticksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11212) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` 
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:11212) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

已经是具有空构造函数、具有所有字段、getter 和 setter 的构造函数的模型类。

这是我的 SubQuestionsRepository 接口。

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;

}

这是我的控制器类的一部分。

public interface SubQuestionsRepository extends Serializable,JpaRepository<Questions,Integer>{

}

但是当我调用“getallnestedques()”方法时,它给出了以下错误

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();
    }
}

我如何解决这个问题?

解决方法

您可以尝试以下解决方案吗?

需要将 answers 列声明为 Lob,如下例所示:

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;

import org.json.JSONArray;

@Entity
public class SubQuestions implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "sub_questionId",nullable = false,columnDefinition = "INT(11) UNSIGNED")
    private Integer sub_questionId;

    @Lob
    @Column
    @Convert(converter = JSONArrayConverter.class)
    private JSONArray answers;
}

属性转换器JSONArrayConverter在存储到数据库之前将JSONArray对象转换为String,并在从数据库读取值后转换为JSONArray

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

import org.json.JSONArray;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

@Converter(autoApply = true)
public class JSONArrayConverter implements AttributeConverter<JSONArray,String> {

    private static final Logger logger = (Logger) LoggerFactory.getLogger(JSONArrayConverter.class);

    @Override
    public String convertToDatabaseColumn(JSONArray array)
    {
        String data = null;
        try
        {
            data = array.toString();
        }
        catch (final Exception e)
        {
            logger.error("JSON writing error",e);
        }

        return data;
    }

    @Override
    public JSONArray convertToEntityAttribute(String data)
    {
        JSONArray array = null;

        try
        {
            array = new JSONArray(data);
        }
        catch (final Exception e)
        {
            logger.error("JSON reading error",e);
        }

        return array;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?