首页 > 解决方案 > org.postgresql.util.PSQLException:错误:列“check_state”的类型为 check_state,但表达式的类型为字符变化

问题描述

我在我的项目中使用 Spring Data JDBC,但我无法持久化具有与我的数据库中的 Postgres 枚举对应的枚举属性的对象。

我有一个与此类似的 PutMapping:

@PutMapping(value = "/{buildingId}/rooms/{roomId}")
  public Room updateRoom(@PathVariable Long buildingId, @PathVariable Long roomId,
                         @RequestBody Set<Quad> quads) {
    Optional<Room> toUpdate = roomRepo.findById(roomId);
    if (toUpdate.isPresent()) {
      Room room = toUpdate.get();
      room.setQuads(quads);
      roomRepo.save(room);
      return room;
    } else {
      ...
    }

  }

使用与此类似的 Quad 实体:

@Data
@Entity
@Table(value = "quads")
public class Quad {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private Long roomId;

  private String quadLabel;

  private CheckState checkState;

  public Quad() {
    checkState = CheckState.UNCHECKED;
  }
}

使用相应的 CheckState 枚举:

public enum CheckState {
  UNCHECKED,
  CHECKED_DAMAGED,
  CHECKED_INTACT,
  RECHECK_NEEDED,
}

但是,当我尝试使用以下正文向此端点发送 PUT 请求时:

{
  ...
  "checkState": "UNCHECKED",
  ...
}

我收到以下错误:

org.postgresql.util.PSQLException: ERROR: column "check_state" is of type check_state but expression is of type character varying

Hint: You will need to rewrite or cast the expression.

我试图发送的 PUT 映射的主体似乎没有正确映射到正确的 Postgres 枚举,但我似乎无法弄清楚如何重写或转换表达式,就像使用 Repo.save 的提示所说的那样() 方法。

标签: javaspringpostgresqlenumsspring-data-jdbc

解决方案


推荐阅读