user10608855
user10608855

Reputation: 119

How to post ManyToOne entity using JSON format

I've two entities issue and user I would like to assign the user to an issue but when trying to do that using this JSON the user_id in the issues table is NULL.

@Data
@Entity
@Table(name = "issues")
public class Issue {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column
    private String number;
    @Column
    private String title;
    @Column
    private String description;
    @Column
    @Enumerated(EnumType.STRING)
    private State state;
    @JsonIgnore
    @CreationTimestamp
    @Column
    private Timestamp createDate;
    @JsonIgnore
    @UpdateTimestamp
    @Column
    private Timestamp modifyDate;
    @ManyToOne(targetEntity = User.class)
    @JoinColumn
    private User user;

    public Issue() {

    }
@Data
@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column
    private String fullName;
    @Column
    private String username;
    @Column
    private String email;
    @Column
    private String password;
    @Column
    @Enumerated(EnumType.STRING)
    private Role role;
    @JsonIgnore
    @CreationTimestamp
    @Column
    private Timestamp createDate;
    @JsonIgnore
    @UpdateTimestamp
    @Column
    private Timestamp modifyDate;

    public User() {

    }

    public User(String fullName, String username, String email, String password, Role role) {
        this.fullName = fullName;
        this.username = username;
        this.email = email;
        this.password = password;
        this.role = role;
    }

Firstly I've created an user without any issues, but do not know how to manage with the issue here is the JSON I am using via postman.

{
    "number": "3",
    "title": "Create an working service",
    "description": "The problem is that we do not have an working service.",
    "state": "NEW",
    "user_id": "1"
}

This part is responsible for saving the issue.

    public void save(Issue issue) {
        if (issue == null)
            return;
        Issue actual = issueRepository.findByNumber(issue.getNumber());
        if (actual != null) {
            actual.setNumber(issue.getNumber());
            actual.setTitle(issue.getTitle());
            actual.setDescription(issue.getDescription());
            actual.setState(issue.getState());
            actual.setUser(issue.getUser());
            issueRepository.save(actual);
        } else {
            issueRepository.save(issue);
        }
    }

In the controller I just have @Valid @RequestBody Issue issue and the service saves the issue.

Upvotes: 2

Views: 4115

Answers (3)

Sanjay
Sanjay

Reputation: 2503

You need to post json like this.

{
    "number": "3",
    "title": "Create an working service",
    "description": "The problem is that we do not have an working service.",
    "state": "NEW",
    "user":{
         "id":1
     }
}

Here issue have join column of user so with user entity you have to pass userid. Your save would be like this.

 public void save(Issue issue) {
        if (issue == null)
            return;
        Issue actual = issueRepository.findByNumber(issue.getNumber());
        if (actual != null) {
            actual.setNumber(issue.getNumber());
            actual.setTitle(issue.getTitle());
            actual.setDescription(issue.getDescription());
            actual.setState(issue.getState());
            actual.setUser(userRepositoy.findById(issue.getUser().getId()));
            issueRepository.save(actual);
        } else {
            issueRepository.save(issue);
        }
    }

Upvotes: 2

GnanaJeyam
GnanaJeyam

Reputation: 3170

user_id key was not presented in your Item class. As per entity mapping, Please change your JSON to this.

{
"number": "3",
 "title": "Create an working service", "description": "The problem is that we do not have an working service.", 
"state": "NEW",
"user":{
"fullName":"USER",
"email":"[email protected]",
"username":"user"
}
}

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691785

What you're sending as JSON is not an Issue.

First because it does not represent a database-persistent issue (and that's what the Issue class is for), but rather the JSON structure the public API expects from its clients.

Second because an Issue doesn't have any field named user_id, but your JSON does. So use a different class than Issue, which actually matches with the JSON structure that the API expects, and thus has a user_id property.

Then use this user_id to find the User by its ID using the UserRepository, and set that User into the Issue you're creating.

I would rename user_id to userId to respect the Java conventions, too.

Upvotes: 2

Related Questions