如何解决使用 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 举报,一经查实,本站将立刻删除。