空外键(Springboot、Hibernate、Postman)

cha*_*cko 5 spring-data-rest spring-boot postman

我正在使用 Springboot 和 Hibernate,我想使用 POST 请求保存一个新的“帖子”到我的数据库。我想强调的一件事是我正在使用依赖项“spring-boot-starter-data-rest”。

数据库架构(MySQL): 舍马:

类用户:

@Entity
@Table(name="user")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id", nullable = false)
    public int id;

    @OneToMany(mappedBy = "user_id_fk")
    public Set<Post> posts;

    @Column(name="email")
    private String email;

    @Column(name="username")
    private String username;

    @Column(name="password")
    private String password;

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

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

    @Column(name="create_time")
    protected Date createTime;

    @Column(name="type")
    private String accountType;

    public User() {
        this.createTime = new java.util.Date();
    }

    public User(String email, String username, String password, String firstName, String lastName, Date createTime, String accountType) {
        this.email = email;
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.createTime = createTime;
        this.accountType = accountType;
        this.createTime = new java.util.Date();
    }

    public User(int id, String email, String username, String password, String firstName, String lastName, Date createTime, String accountType) {
        this.id = id;
        this.email = email;
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.createTime = createTime;
        this.accountType = accountType;
        this.createTime = new java.util.Date();
    }
Run Code Online (Sandbox Code Playgroud)

加上 Getters & Setters & toString()。

班级职位:

@Entity
@Table(name="post")
public class Post implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    public int id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "user_id_fk", nullable = false)
    public User user_id_fk;

    @Column(name="comment")
    private String comment;

    @Column(name="likes")
    private int likes;

    @Column(name="dislike")
    private int dislike;

    @Column(name="create_time")
    protected Date createTime;

    public Post() {
        this.createTime = new java.util.Date();
    }

    public Post(String comment, int likes, int dislike, User user_id_fk) {
        this.user_id_fk = user_id_fk;
        this.comment = comment;
        this.likes = likes;
        this.dislike = dislike;
        this.createTime = new java.util.Date();
    }

    public Post(int id, User user_id_fk, String comment, int likes, int dislike) {
        this.id = id;
        this.user_id_fk = user_id_fk;
        this.comment = comment;
        this.likes = likes;
        this.dislike = dislike;
        this.createTime = new java.util.Date();
    }
Run Code Online (Sandbox Code Playgroud)

加上 Getters & Setters & toString()。

发布请求(我使用 Postman 发送请求):

{
"comment" : "This is a comment",
"likes" : 123,
"dislike" : 1,
"user_id_fk" :
[
    {
        "id" : 1
    }
]
}
Run Code Online (Sandbox Code Playgroud)

在“user_id_fk”的请求中,我尝试使用 [ {"id" : 1 } ] 和 { "id" : 1 } 但结果是一样的。

问题:

当我从控制器执行完全相同的代码时,一切正常。请记住,我正在使用依赖项“spring-boot-starter-data-rest”。此外,当我在没有“optional = false”和“nullable = false”将数据插入数据库但“user_id_fk”为空的情况下执行代码时:(。

我得到的错误:

not-null property references a null or transient value : com.citizen.citizen.entity.Post.user_id_fk; 
nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.citizen.citizen.entity.Post.user_id_fk]
Run Code Online (Sandbox Code Playgroud)

这意味着外键("user_id_fk")为空但不应为空。任何帮助将不胜感激。

cha*_*cko 2

我只是删除了依赖项“spring-boot-starter-data-rest”,然后通过创建自定义休息解决了问题,一切正常。吻!