简体   繁体   English

如何使用 Spring Boot 将类对象的列表存储到 MySql 中

[英]How to store a List of a class object into MySql using Spring boot

I want to store a List of class : RestApiResponse into MySql.我想将类列表:RestApiResponse 存储到 MySql 中。 But getting below error:但得到以下错误:

org.hibernate.HibernateException: Could not determine a type for class: com.try.sreapi.beans.RestApiResponse

Below are my classes:下面是我的课:

Entity class : SREAPITestingHistory.java实体类: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;
    }

    
}

Repository Class: SREAPITestingRepository.java存储库类: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);

}

When I am trying to add values for response_body which is actually a List of RestApiResponse class and I am getting Could not determine a type for class: com.try.sreapi.beans.RestApiResponse exception当我尝试为 response_body 添加值时,它实际上是 RestApiResponse 类的列表,并且我得到了无法确定类的类型:com.try.sreapi.beans.RestApiResponse 异常

From Official doc来自官方文档

A Lob may be either a binary or character type. Lob 可以是二进制或字符类型。

The Lob type is inferred from the type of the persistent field or property, and except for string and character-based types defaults to Blob. Lob 类型是从持久字段或属性的类型推断出来的,除字符串和基于字符的类型外,默认为 Blob。

Example 1:示例 1:

@Lob @Basic(fetch=LAZY) @Column(name="REPORT") @Lob @Basic(fetch=LAZY) @Column(name="REPORT")
String report;字符串报告;

Example 2:示例 2:

@Lob @Basic(fetch=LAZY) @Column(name="EMP_PIC", columnDefinition="BLOB NOT NULL") protected byte[] pic; @Lob @Basic(fetch=LAZY) @Column(name="EMP_PIC", columnDefinition="BLOB NOT NULL") protected byte[] pic;

So you can convert your list of data into json string or bytes to store.因此,您可以将数据列表转换为要存储的 json 字符串或字节。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Z2A2D595E6ED9A0B24F027F2B63B13 将 json object 存储在 mysql 的列中 - how to store json object in a column of mysql using spring boot/java 如何储存清单 <Object> 使用Spring Boot的Redis中的值类型? - How to store a List<Object> type of value in Redis using Spring boot? Mysql 文档存储和 Spring 引导 - Mysql Document Store and Spring Boot 如何从Spring boot Dependency Injection中的对象列表中找到特定的类对象 - How to find a particular class object from a List of Object in Spring boot Dependency Injection 如何使用 Hibernate / Spring boot 在 mysql 中将 JSON 文件存储为 JSON 数据类型? - How to store JSON file as a JSON datatype in mysql using Hibernate / Spring boot? 尝试将 object 添加到列表并将其保存在 Spring 启动上的 MySQL 中时出现 UnsupportedOperationException - UnsupportedOperationException when trying to add object to a list and save it in MySQL on Spring Boot 如何在 Spring 引导中使用 @Valid 验证 object? - How validate an object using @Valid in Spring Boot? Spring 引导:如何测试使用 gson ZA8CFDE6331BD59EB2AC96F8911C4B666 的 class - Spring Boot: How to test a class which is using gson object using autowiring 无法使用 spring 引导中的测试 class 更新 MySQL 中的数据 - Not able to update data in MySQL using test class in spring boot Spring Boot + Hibernate + Mysql 如何生成类java - Spring Boot + Hibernate + Mysql how to generate class java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM