无法让布尔值使用 spring boot 和 postgresql 在插入上工作



我在使用 spring boot 和 postgresql 处理布尔值时遇到了一些问题。我删除了一个字段,该字段在实体 bean 和数据库表中都定义为布尔值。看起来在选择抛出其余 API 时,我被返回了一个 JSON 字符而不是布尔值。插入时,无论我尝试布尔值还是字符(即从查询的响应构建正文(,我都会收到错误。 我正在运行OpenJDK 11.PostgreSQL JDBC驱动程序42.2.5。野外 15.

我已经尝试使用在选择(获取(和JSON布尔值true上返回的字符"t"。我所依赖的 json 布尔值的定义

https://json-schema.org/understanding-json-schema/reference/boolean.html

这是表创建

CREATE TABLE TRANSFER_PARTITIONED_IMAGE (
TRANSFER_PARTITIONED_IMAGE_ID     INTEGER NOT NULL,
IMAGE_NAME               VARCHAR(50) NOT NULL,
REQUESTED_PART_SIZE_MB   INTEGER NOT NULL,
SIZE_BYTES               INTEGER NOT NULL,
IMAGE_MD5_HASH           VARCHAR(100),
NUMBER_PARTITIONS        INTEGER,
DELETED                  BOOLEAN  NOT NULL
);

这是我用来从 postgresql 命令行在表中放置一行的插入

crowbar=> insert into transfer_partitioned_image       
values(45,'foofoobarbarbarbarsnafu',10,10,'aa',10,true);

JSON 在选择时返回。请注意,它为布尔字段返回一个字符"t"。

{
"transferPartitionedImageId": 45,
"imageName": "foofoobarbarbarbarsnafu",
"requestedPartSizeMB": 10,
"sizeBytes": 10,
"imageMD5Hash": "aa",
"numberPartitions": 10,
"deleted": "t"
}

如果我尝试使用"t"作为布尔值进行插入,则会出现服务器 500 错误

{
"timestamp": "2019-10-07T13:48:38.077+0000",
"message": "could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement",
"details": "uri=/crowbar/data/rit/transferPartitionedImage"
}

从日志文件

2019-10-07 13:48:38,075 INFO  [stdout] (default task-1)     insert
2019-10-07 13:48:38,075 INFO  [stdout] (default task-1)     into
2019-10-07 13:48:38,075 INFO  [stdout] (default task-1)           transfer_partitioned_image
2019-10-07 13:48:38,075 INFO  [stdout] (default task-1)         (deleted, image_md5_hash, image_name, number_partitions, requested_part_size_mb, size_bytes)
2019-10-07 13:48:38,075 INFO  [stdout] (default task-1)     values
2019-10-07 13:48:38,075 INFO  [stdout] (default task-1)         (?, ?, ?, ?, ?, ?)
2019-10-07 13:48:38,076 DEBUG [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-1) could not execute statement [n/a]: org.postgresql.util.PSQLException: ERROR: column "deleted" is of type boolean but expression is of type character varying
Hint: You will need to rewrite or cast the expression.

如果我使用 JSON 布尔值尝试同样的事情(尝试了大写和小写 TRUE(

{
"transferPartitionedImageId": 1111,
"imageName": "foofoobarfoofoobarsnafusnafu",
"requestedPartSizeMB": 10,
"sizeBytes": 10,
"imageMD5Hash": "aa",
"numberPartitions": 10,
"deleted": true
}

我收到 400 错误请求错误

Status Code: 400 Bad Request
Connection: keep-alive
Date: Mon, 07 Oct 2019 13:53:13 GMT
Transfer-Encoding: chunked

从日志

019-10-07 13:53:13,160 DEBUG [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] (default task-1) Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.Character` out of VALUE_TRUE token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.Character` out of VALUE_TRUE token
at [Source: (PushbackInputStream); line: 8, column: 14] (through reference chain: com.ticomgeo.crowbar.data.entity.TransferPartitionedImage["deleted"])]

我的实体 Bean 定义如下所示

Entity Bean definition
@Entity
public class TransferPartitionedImage implements Serializable {
private static final long serialVersionUID = -3444719649254510891L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer transferPartitionedImageId;
@NotNull
@Size(min = 10, max = 50, message = "Image name must be 10  to 50 characters")
@Column(name = "IMAGE_NAME", nullable = false, length = 50)
private String imageName;
@NotNull
@Column(name = "REQUESTED_PART_SIZE_MB", nullable = false)
private Integer requestedPartSizeMB;
@Column(name = "SIZE_BYTES")
private Integer sizeBytes;
@Column(name = "IMAGE_MD5_HASH")
private String imageMD5Hash;
@Column(name = "NUMBER_PARTITIONS")
private Integer numberPartitions;
@Column (name = "DELETED")
private Boolean deleted;
public Integer getTransferPartitionedImageId() {
return transferPartitionedImageId;
}
public void setTransferPartitionedImageId(Integer   transferPartitionedImageId) {
this.transferPartitionedImageId = transferPartitionedImageId;
}
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public Integer getRequestedPartSizeMB() {
return requestedPartSizeMB;
}
public void setRequestedPartSizeMB(Integer requestedPartSizeMB) {
this.requestedPartSizeMB = requestedPartSizeMB;
}
public Integer getSizeBytes() {
return sizeBytes;
}
public void setSizeBytes(Integer sizeBytes) {
this.sizeBytes = sizeBytes;
}
public String getImageMD5Hash() {
return imageMD5Hash;
}
public void setImageMD5Hash(String imageMD5Hash) {
this.imageMD5Hash = imageMD5Hash;
}
public Integer getNumberPartitions() {
return numberPartitions;
}
public void setNumberPartitions(Integer numberPartitions) {
this.numberPartitions = numberPartitions;
}
public Boolean getDeleted() {
return deleted;
}
public void setDeleted(Boolean deleted) {
this.deleted = deleted;
}
public Boolean isDeleted() {
return deleted ;
}

我本来希望选择返回一个 JSON 布尔值,即 已删除:真而不是
字符"t"。我还希望将选择中返回的内容的示例粘贴到带有新主键的帖子正文中,以便在插入中工作。

问题与我在容器中部署 war 文件的方式有关。在早期版本中,该字段是字符。我修改了实体 bean 以使其成为布尔值,并将 war 文件复制到 wildfly 实例的部署目录中。显然,在这种情况下,JPA 注释不会更新,因此未部署对实体 Bean 的更改。重新启动 wildfly 实例消除了此问题,而无需进行任何代码更改。

尝试使用注释

@GeneratedValue (strategy = GenerationType.IDENTITY)

最新更新