Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null Foreign Key (Springboot, Hibernate, Postman)

I am using Springboot with Hibernate and I would like to save a new “post” using a POST request to my database. One thing that I would like to highlight is that I am using the dependency “spring-boot-starter-data-rest”.

Schema of the database (MySQL): Shema:

Class User:

@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();
    }

Plus the Getters & Setters & toString().

Class Post:

@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();
    }

Plus the Getters & Setters & toString().

Post request (I'm using Postman to send the request):

{
"comment" : "This is a comment",
"likes" : 123,
"dislike" : 1,
"user_id_fk" :
[
    {
        "id" : 1
    }
]
}

In the request at the "user_id_fk" I tried with [ {"id" : 1 } ] and with { "id" : 1 } but the result was the same.

Issue:

When I am executing exactly the same code from my controller everything works are excepted. Bear in mind that I am using the dependency “spring-boot-starter-data-rest”. Also, when I am executing the code without the “optional = false” and “nullable = false” is inserting the data into the database but the “user_id_fk” is null :(.

The error that I am getting:

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]

That means that the foreign key ("user_id_fk") is null but should not be null. Any help will be greatly appreciated.

like image 903
Nicholas Avatar asked Oct 15 '22 01:10

Nicholas


2 Answers

I just remove the dependency "spring-boot-starter-data-rest" and I solved the issue by creating my custom rest and everything works. Kisses!

like image 82
Nicholas Avatar answered Oct 29 '22 13:10

Nicholas


According to this article, you should make user_id_fk nullable and then:

  1. Send POST to create User
  2. Send second POST to create Post
  3. Send PUT to create a relation between the two.

This article states the same. And the documentation only mentions handling associations via association links.

like image 30
Konrad Botor Avatar answered Oct 29 '22 12:10

Konrad Botor