简体   繁体   English

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

[英]Cannot get booleans to work on insert using spring boot and postgresql

I'm having some problems working with booleans with spring boot and postgresql.我在使用 spring 引导和 postgresql 的布尔值时遇到了一些问题。 I have a field deleted that is defined as a boolean in both the entity bean and the database table.我删除了一个在实体 bean 和数据库表中定义为 boolean 的字段。 It looks like on a select throw the rest API I am being returned a JSON char instead of a boolean. It looks like on a select throw the rest API I am being returned a JSON char instead of a boolean. On insert whether I try a boolean or a char ( ie building the body from a response from a query) I get an error.在插入时,无论我尝试使用 boolean 还是 char(即根据查询的响应构建正文),我都会收到错误消息。 I am running on OpenJDK 11.PostgreSQL JDBC Driver 42.2.5.我在 OpenJDK 11.PostgreSQL JDBC 驱动程序 42.2.5 上运行。 Wildflt 15.野飞 15.

I've tried both using char "t" as returned on the select (get) and JSON boolean true.我已经尝试过使用 select (get) 和 JSON boolean 上返回的字符“t”。 Definition of json boolean I relied on json boolean 的定义我依赖

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

Here's the table create这是表创建

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
);

Here's the insert I used to put a row in the table from the postgresql command line这是我用来从 postgresql 命令行在表中插入一行的插入

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

JSON returned on a select. JSON 在 select 上返回。 Note it returns a char "t" for the boolean field.请注意,它为 boolean 字段返回一个字符“t”。

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

If I try to insert using the "t" for the boolean value I get a server 500 error如果我尝试使用 boolean 值的“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"
}

from the log file从日志文件

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.

If I try the same thing using a JSON boolean ( tried both an upper case and lower case TRUE)如果我使用 JSON boolean 尝试相同的事情(尝试了大写和小写 TRUE)

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

I get a 400 bad request error我收到 400 错误请求错误

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

From the log从日志

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"])]

My entity bean definition is shown below我的实体 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 ;
}

I would have expected the select to have returned a JSON boolen ie deleted: true我本来希望 select 返回 JSON 布尔值,即删除:true
Instead of a char "t".而不是一个字符“t”。 I also would have expected that pasting an example of what was returned on a select into the body of a post with a new primary key to have worked on an insert.我还期望将 select 返回的示例粘贴到具有新主键的帖子正文中以进行插入。

The problem was related to how I was deploying the war file in the container.问题与我如何在容器中部署 war 文件有关。 In an earlier version the field was a char.在早期版本中,该字段是一个字符。 I modified the entity bean to make it a Boolean and copied the war file into the wildfly instance's deployment directory.我修改了实体 bean 使其成为 Boolean 并将 war 文件复制到 wildfly 实例的部署目录中。 Apparently the JPA annotations are not updated in that case so the change to the Entity bean was not deployed.显然 JPA 注释在这种情况下没有更新,因此没有部署对实体 bean 的更改。 Restarting the wildfly instance eliminated the issue without any code changes.重新启动 wildfly 实例消除了该问题,无需任何代码更改。

Try with annotation尝试使用注释

@GeneratedValue (strategy = GenerationType.IDENTITY)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM