无法使用布尔值使用Spring Boot和Postgresql进行插入操作

问题描述 投票:0回答:1

我在使用Spring Boot和Postgresql使用布尔值时遇到一些问题。我删除了在实体bean和数据库表中都定义为布尔值的字段。似乎在选择抛出其余API时,我将返回JSON字符而不是布尔值。在插入时,无论我尝试布尔还是字符(即根据查询的响应构建主体),都会收到错误消息。我在OpenJDK 11.PostgreSQL JDBC驱动程序42.2.5上运行。 Wildflt 15。

我已经尝试过使用select(get)返回的char“ t”和JSON布尔值true。我依赖的json boolean的定义

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布尔值,即已删除:true而不是char“ t”。我也希望将一个选择返回的示例粘贴到帖子的主体中,并使用一个新的主键进行插入。

json database postgresql hibernate spring-boot
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.