Home > Blockchain >  Why data is insert while update data
Why data is insert while update data

Time:12-03

When request for PUT it's save record in child entity but not update. What's wrong doing by me i can't understand. in my database record actual id for three book is 5,6,7 but when update data through postman it save new book with id 20,21,22.

Entity

public class Author {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer author_id;
    private String name;
    private String language;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "author")
    private Set<Book> book;

    // getter setter
}

public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String title;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "author_id")
    private Author author;

    // getter setter
}

Service

@Override
public Course updateCourseBook(Course course) {
    return this.courseRepo.save(course);
}

Controller

@RequestMapping(value = "/coursebook", method = RequestMethod.PUT)
public ResponseEntity<Course> updateCourseBook(@RequestBody Course course) {
     for(Book book: course.getBooks())
     {
         book.setCourse(course);
     }

     Course updateCourse = this.pojoService.updateCourseBook(course);
     if(updateCourse != null)
        return ResponseEntity.ok(updateCourse);
     return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}

Request

enter image description here

Output

enter image description here

CodePudding user response:

My Problem is solved. I can not declare book id while inserting data.

{
    "id": 3,
    "name": "Maths",
    "description": "Books about mathematics",
    "books": [
        {
            "book_id": 5, // forget this
            "title": "Infinite Powers"
        },
        {
            "book_id": 6, // forget this
            "title": "Mathematics For Machine Learning"
        },
        {
            "book_id": 7, // forget this
            "title": "Discrete Mathematics2"
        }
    ]
}
  • Related