You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot JPA存储Invoice对象到invoice_history表异常解决

问题场景

我需要将Invoice类型的对象存储到invoice_history表中,执行存储操作后该字段存储值为null,问题运行参考截图:
问题运行截图

初始实体类代码

对应实体类(model)代码如下:

public class InvoiceHistory {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;
    @Column(name = "updated_date")
    private Timestamp updatedDate;
    @Transient
    @Column(name = "invoice_info")
    private Invoice invoiceHistory;
    @ManyToOne
    @JoinColumn(name = "invoice_id")
    private Invoice invoice;
    @ManyToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;
}

移除@Transient注解时,会抛出以下错误:

Could not determine type for: com.example.invoices.model.Invoice, at table: invoice_history, for columns: [org.hibernate.mapping.Column(invoice_info)]

初始控制器代码

对应控制器(controller)代码如下:

@PostMapping("/save")
public ResponseEntity<InvoiceHistory> addHistory(@RequestBody InvoiceHistoryDTO invoice) {
    try {
        InvoiceHistory newInvoice = invoiceHistoryService
                .saveInvoiceHistory(new InvoiceHistory(invoice.getUpdatedDate(), invoice.getInvoiceInfo(), invoice.getInvoiceInfo().getEmployee()));
        return new ResponseEntity<>(newInvoice, HttpStatus.CREATED);
    } catch (Exception e) {
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

问题更新

查阅jsonb数据类型相关资料后,我已在数据库表中创建了对应jsonb类型字段,表结构参考截图:
表结构截图

随后我将实体类代码修改为:

@Column(name = "invoice_data")
@Type(type = "jsonb")
private Invoice invoiceHistory;

同时添加了类型定义配置:

@TypeDefs({
        @TypeDef(name = "string-array", typeClass = StringArrayType.class),
        @TypeDef(name = "int-array", typeClass = IntArrayType.class),
        @TypeDef(name = "json", typeClass = JsonStringType.class),
        @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})

修改后代码运行无报错,但数据库对应字段仍然没有数据写入,请问该问题如何解决?


内容的提问来源于stack exchange,提问作者Talia Dianal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 12:36:20