gpt4 book ai didi

java - Spring 启动+ Spring 数据休息: post json null value in column "first_name" violates not-null constraint

转载 作者:行者123 更新时间:2023-12-02 03:06:28 26 4
gpt4 key购买 nike

当我创建一个 POST 方法进行这样的测试时(使用 postman ):

{
"firstName": "Yue",
"lastName": "Yang",
"email": "g1enyy0ung@gmail.com",
"password": "123"
}

我收到的回复是:

{
"cause": {
"cause": {
"cause": null,
"message": "ERROR: null value in column \"first_name\" violates not-null constraint\n Detail: Failing row contains (2, null, null, null, f, null, null)."
},
"message": "could not execute statement"
},
"message": "could not execute statement; SQL [n/a]; constraint [first_name]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
}

我对此感到非常困惑,我不知道为什么firstName为空,这是我定义实体的内容:

@Entity
@Table(name = "app_user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Getter
@Setter
@Column(columnDefinition = "SERIAL")
private Long id;

@Column(name = "first_name")
@Getter
@Setter
private String firstName;

@Column(name = "last_name")
@Getter
@Setter
private String lastName;

@Getter
@Setter
private String email;

@Column(name = "pass")
@Getter
@Setter
private String password;

@Getter
private boolean admin;

@Column(name = "last_login", columnDefinition = "TIMESTAMPTZ")
@Temporal(TemporalType.TIMESTAMP)
@Getter
private Date lastLogin;

public User() {}

public User(String firstName, String lastName, String email, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
}

SQL是(使用PostgreSQL,我还使用flyway在Spring Boot运行时迁移表):

CREATE TABLE "app_user" (
id SERIAL PRIMARY KEY,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
email VARCHAR(30) NOT NULL,
admin BOOLEAN DEFAULT FALSE,
last_login TIMESTAMPTZ,
pass VARCHAR(32) NOT NULL
);

hibernate总是执行这个SQL:

Hibernate: insert into app_user (admin, email, first_name, last_login, last_name, pass) values (?, ?, ?, ?, ?, ?)

为什么会发生这种情况?我带着这个问题在google上搜索,但我发现几乎所有的搜索结果都使用嵌入数据库,比如H2。而且几乎所有的教程都不使用@Column注释,那么这个问题与Hibernate有关吗?我是 Hibernate 和 spring boot 的新手,现在我正在阅读文档以找到这个问题的解决方案。

希望有人能帮我纠正这个问题。谢谢!

更新:application.yml =>

spring:
datasource:
url: jdbc:postgresql://localhost:5432/thoughtplus
username: g1eny0ung
password: zhuzhu59y
platform: POSTGRESQL
jpa:
hibernate:
ddl-auto: validate
show-sql: true
data:
rest:
base-path: /api

server:
port: 8888

最佳答案

我尝试使用 Maven 构建并遇到类似的问题。进行maven build后解决了。

我认为问题不在于 spring 默认配置,因为我能够成功从存储库中检索实体。但是,当我将实体作为 Controller 的响应发送时, jackson 映射器在转换实体时出现问题。

示例:

      @GetMapping("/")
public User getUser() throws JsonProcessingException{
User user=userSer.getUser(100);
System.out.println(user.toString());
ObjectMapper mapper=new ObjectMapper();
System.out.println("Object mapper "+mapper.writeValueAsString(user));

return user;
}

输出:

User [userId=100, userName=barath]
Object mapper {}

GitHub 链接:Spring-data-lombok

关于java - Spring 启动+ Spring 数据休息: post json null value in column "first_name" violates not-null constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41646758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com