【问题标题】:Can not store list of objects as single column JSON无法将对象列表存储为单列 JSON
【发布时间】:2022-12-06 20:42:15
【问题描述】:

我试图将整个对象数组存储到我的 oracle 数据库的一个字段中,我指的是this 问题的解决方案,但它一直给我 Can not set java.lang.String field xxx.demo.Models.Sensors.amplitudos to xxx.demo.Models.Sensors 错误,我已经检查了 json 主体和实体类,但我找不到错误

这是我的代码

entity

@Entity
@Table(name = "SENSOR")
public class Sensor implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "TIMERECEIVED")
    private Timestamp timereceived;

    @Column(name = "SENSORS")
    private Sensors[] sensors;

    @Column(name = "LOC")
    private String location;

    public Sensor() {

    }

    public Sensor(Timestamp timereceived, Sensors[] sensors, String location) {
        this.timereceived = timereceived;
        this.sensors = sensors;
        this.location = location;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Timestamp getTimereceived() {
        return timereceived;
    }

    public void setTimereceived(Timestamp timereceived) {
        this.timereceived = timereceived;
    }

    public Sensors[] getSensors() {
        return sensors;
    }

    public void setSensors(Sensors[] sensors) {
        this.sensors = sensors;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

}

Sensors class

@Embeddable
public class Sensors {
    private String amplitudos;
    private Double displacement;
    private String frequencies;
    private Integer sensorId;

    public Sensors() {

    }

    public Sensors(String amplitudos, Double displacement, String frequencies, Integer sensorId) {
        this.amplitudos = amplitudos;
        this.displacement = displacement;
        this.frequencies = frequencies;
        this.sensorId = sensorId;
    }

    public String getAmplitudos() {
        return amplitudos;
    }

    public void setAmplitudos(String amplitudos) {
        this.amplitudos = amplitudos;
    }

    public Double getDisplacement() {
        return displacement;
    }

    public void setDisplacement(Double displacement) {
        this.displacement = displacement;
    }

    public String getFrequencies() {
        return frequencies;
    }

    public void setFrequencies(String frequencies) {
        this.frequencies = frequencies;
    }

    public Integer getSensorId() {
        return sensorId;
    }

    public void setSensorId(Integer sensorId) {
        this.sensorId = sensorId;
    }

}

我的 json 体

{
    "timereceived": "2022-11-29T12:04:42.166",
    "sensors": [
        {
            "amplitudos": "a1#a2#a3#a4",
            "displacement": 0.002,
            "frequencies": "f1#f2#f3#f4",
            "sensorid": 1
        },
        {
            "amplitudos": "a1#a2#a3#a4",
            "displacement": 0.002,
            "frequencies": "f1#f2#f3#f4",
            "sensorid": 2
        },
        {
            "amplitudos": "a1#a2#a3#a4",
            "displacement": 0.002,
            "frequencies": "f1#f2#f3#f4",
            "sensorid": 3
        },
        {
            "amplitudos": "a1#a2#a3#a4",
            "displacement": 0.002,
            "frequencies": "f1#f2#f3#f4",
            "sensorid": 4
        }
    ],
    "location": "lokasi"
}

我的控制器

    @PostMapping("/sendData")
    public ResponseEntity sendData(@RequestBody Sensor sensor) {
        Sensor newSensor = sensorRepository.save(sensor);

        System.out.println(newSensor);
        return ResponseEntity.ok("Sensor received");

    }

我已尝试检查所有可能的解决方案,但问题并未解决,我的期望是数据存储在 json 正文中 sensors 字段的 1 列中

【问题讨论】:

  • 你能分享完整的堆栈跟踪吗?

标签: java spring-boot jpa


【解决方案1】:

我认为问题出在 JPA 映射上,而不是控制器上。

您正在使用 @Embeddable,这通常会在您的主表中生成一组列。如果它是@Embeddable 对象的集合,您可以使用@ElementCollection 将它映射到带有外键的单独表。

但是,您希望将传感器集合作为单个 JSON 字符串存储在主表的单个列中。为此,您不需要 @Embeddable 注释。您需要编写自定义转换器以将传感器集合转换为 JSON。

public class SensorsConverter implements AttributeConverter<List<Sensors>, String> {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String convertToDatabaseColumn(List<Sensors> sensors) {

        return objectMapper.writeValueAsString(sensors);
    }

    @Override
    public List<Sensors> convertToEntityAttribute(String sensorsJSON) {

        return objectMapper.readValue(sensorsJSON, new TypeReference<List<Sensors>>() {});
    }

}

然后你可以在你的实体类中使用它:

@Column(name = "SENSORS")
@Convert(converter = SensorsConverter.class)
private List<Sensors> sensors;

【讨论】:

    猜你喜欢
    • 2021-09-12
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2021-12-23
    • 2014-06-12
    • 2021-08-01
    相关资源
    最近更新 更多