Java 如何使用SpringBoot将类对象列表存储到MySql中

Java 如何使用SpringBoot将类对象列表存储到MySql中,java,spring-boot,jpa,spring-data-jpa,Java,Spring Boot,Jpa,Spring Data Jpa,我想将class:RestApiResponse的列表存储到MySql中。但下面是一个错误: org.hibernate.HibernateException: Could not determine a type for class: com.try.sreapi.beans.RestApiResponse 以下是我的课程: 实体类:SREAPITestingHistory.java @NamedQueries(@NamedQuery(name="getSREAPITest

我想将class:RestApiResponse的列表存储到MySql中。但下面是一个错误:

org.hibernate.HibernateException: Could not determine a type for class: com.try.sreapi.beans.RestApiResponse
以下是我的课程:

实体类:SREAPITestingHistory.java

    @NamedQueries(@NamedQuery(name="getSREAPITestHistory.findAll", query="SELECT a FROM SREAPITestingHistory a"))

@SqlResultSetMapping(name="sreapitestinghistoryres",
entities=@EntityResult(entityClass=SREAPITestingHistory.class))

@Entity
@Table(name="sreapi_testing_history_details")
@Transactional
public class SREAPITestingHistory implements Serializable{
    
    private static final long serialVersionUID = -7221709766109001257L;
    
    @Id
    @Column(name="request_time")
    private String request_time;
    
    @Column(name="req_id")
    private String req_id;
    
    @Column(name="app_name")
    private String app_name; 
    
    @Column(name="request_name")
    private String request_name;
    
    @Lob
    @Column(name="response_body")
    private List<RestApiResponse> response;

    public String getRequest_time() {
        return request_time;
    }

    public void setRequest_time(String request_time) {
        this.request_time = request_time;
    }

    public String getReq_id() {
        return req_id;
    }

    public void setReq_id(String req_id) {
        this.req_id = req_id;
    }

    public String getApp_name() {
        return app_name;
    }

    public void setApp_name(String app_name) {
        this.app_name = app_name;
    }


    public String getRequest_name() {
        return request_name;
    }

    public void setRequest_name(String request_name) {
        this.request_name = request_name;
    }

    public List<RestApiResponse> getResponse() {
        return response;
    }

    public void setResponse(List<RestApiResponse> response) {
        this.response = response;
    }

    
}
@namedquerys(@NamedQuery(name=“getSREAPITestHistory.findAll”,query=“从SREAPITestingHistory a中选择a”))
@SqlResultSetMapping(name=“sreapitestinghistoryres”,
entities=@EntityResult(entityClass=SREAPITestingHistory.class))
@实体
@表(name=“sreapi测试历史详细信息”)
@交易的
公共类SREAPITestingHistory实现可序列化{
私有静态最终长serialVersionUID=-722170966109001257L;
@身份证
@列(name=“请求时间”)
私有字符串请求时间;
@列(name=“req\u id”)
私有字符串请求id;
@列(name=“app\u name”)
私有字符串app_name;
@列(name=“request\u name”)
私有字符串请求名称;
@高球
@列(name=“response\u body”)
私有列表响应;
公共字符串getRequest_time(){
返回请求时间;
}
公共无效设置请求时间(字符串请求时间){
this.request\u time=请求时间;
}
公共字符串getReq_id(){
返回请求id;
}
公共无效设置请求id(字符串请求id){
this.req_id=req_id;
}
公共字符串getApp_name(){
返回应用程序名称;
}
公共无效设置应用程序名称(字符串应用程序名称){
this.app\u name=app\u name;
}
公共字符串getRequest_name(){
返回请求名称;
}
public void setRequest_name(字符串请求_name){
this.request\u name=request\u name;
}
公共列表getResponse(){
返回响应;
}
公共无效设置响应(列表响应){
这个。反应=反应;
}
}
存储库类:SREAPITestingRepository.java

@Repository
public interface SREAPITestingRepository extends CrudRepository< SREAPITestingHistory, String> {
    
    @Modifying
    @Transactional
    @Query(value="INSERT into sreapi_testing_history_details (request_time,req_id,app_name,request_name,response_body)"+ "VALUES (:request_time,:req_id,:app_name,:request_name,:response_body)", nativeQuery = true)
    public void setApiTestHistoryDetails(@Param("request_time") String request_time,@Param("req_id") String req_id,@Param("app_name") String app_name,@Param("request_name") String request_name,@Param("response_body") List<RestApiResponse> response_body);

}
@存储库
公共接口SREAPITestingRepository扩展了crudepository{
@修改
@交易的
@查询(value=“插入sreapi_测试_历史_详细信息(请求_时间、请求_id、应用程序名称、请求_名称、响应_正文)”+“值(:请求_时间、:请求_id、:应用程序名称、:请求_名称、:响应_正文)”,nativeQuery=true)
public void setApiTestHistoryDetails(@Param(“请求时间”)字符串请求时间、@Param(“请求id”)字符串请求id、@Param(“应用名称”)字符串应用名称、@Param(“请求名称”)字符串请求名称、@Param(“响应正文”)列表响应正文);
}
当我试图为response_body(实际上是response类的列表)添加值时,我无法确定类的类型:com.try.sreapi.beans.RestApiResponse exception

Lob可以是二进制或字符类型。

Lob类型是从持久性字段或 属性,除字符串和基于字符的类型外,默认为 斑点

例1:

@Lob@Basic(fetch=LAZY)@Column(name=“REPORT”)
字符串报告

例2:

@Lob@Basic(fetch=LAZY)@Column(name=“EMP\u PIC”, columnDefinition=“BLOB NOT NULL”)受保护的字节[]pic


因此,您可以将数据列表转换为json字符串或字节来存储。

您的Entity名为
SREAPITestingHistory
,而您的存储库正在查找
SREAPITesting
。对于实体列表,您可以选择
.saveAll()
,为什么不使用它呢?问题在于类对象。如果我使用.saveAll(),那么这个错误也会出现。@NamanAgarwal你检查过答案了吗?