首页 > 解决方案 > 在 JPA 实体中返回并保留 LinkedHashMap 而不是 Map

问题描述

我试图LinkedHashMap在我的 Spring Boot 项目中坚持我的 JPA 实体。我需要这个,因为与其他接口实现LinkedHashMap相比,保留了插入顺序。Map

在我的ComicJPA 实体中,我有以下字段:

@ElementCollection
@Column(name="price_history", nullable=true)
private Map<String, Float> priceHistory = new LinkedHashMap<String, Float>();

我的Comic实体构造函数LinkedHashMap<String,Float>也将 a 作为参数。

我的 getter 和 setter 方法如下所示:

    public Map<String,Float> getPriceHistory() {
        return priceHistory;
    }

    public void setPriceHistory(Map<String,Float> priceHistory) {
        this.priceHistory = priceHistory;
    }

我的 REST 控制器中有一个方法,允许我编辑任何ComicJPA 实体实例的数据。以下代码负责将价格放入priceHistory Map<String,Float>对象中:

editComic.getPriceHistory().put(WordUtils.capitalizeFully(LocalDate.now().getMonth().toString() 
+ " " + LocalDate.now().getYear()), price);
editComic.setPriceHistory(editComic.getPriceHistory());

有没有办法让我坚持LinkedHashMap<String,Float>JPA?我尝试使用注释该字段,@Lob但它在 PostgreSQL 中导致异常:

org.postgresql.util.PSQLException: Large Objects may not be used in auto-commit mode.

标签: javaspring-bootjpa

解决方案


推荐阅读