簡體   English   中英

使用 spring 引導的一對多單向關系

[英]One-to-many unidirectional relationship using spring boot

我有一個名為 user 的實體 class ,代碼如下:

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

    //one-to-many relationship with borrowed books
    
    @OneToMany(mappedBy="user", cascade = CascadeType.ALL)
    @JoinColumn(name="user_id")
    
    private List<BorrowedBooks> borrowedBooks;
    
    
    


    //define fields
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    
    @Column(name="first_name")
    private String firstName;

    
    // getters and setters go here

    //this is the method used to add the borrowed books
      public void addBorrowedBook(BorrowedBooks borrowedbook) {
        if(borrowedBooks==null) {
            borrowedBooks = new ArrayList<>();
        }
        borrowedBooks.add(borrowedbook);
    }

下面是書籍表的代碼

@Entity
@Table(name="books")
public class Books {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Integer Id; // autogenerated unique values.
    
    @Column(name="title")
    private String title;
    @Column(name="genre")
//getters and setters go here

下面是借書實體 class 的代碼:

   public class BorrowedBooks {

    
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name="id")
    private Integer id;
    
    @Column(name="bookName")
    private String bookName;
    @Column(name="genre")
    private String genre ;
    //getters and setters goes here
}

添加借書代碼的hibernate代碼如下:

User newUser = new User(1,"john doe") //the values are userId,first_name
newUser.addBorrowedBook(new BorrowedBooks(1,"love to code", "computer science")); //values are id,book name, genre
session.save(newUser) // this saves the user along with the borrowed book details with userId as foreignKey into the database

我正在嘗試使用 spring rest controller 來實現此代碼

@RestController
@RequestMapping("/base path goes here")
public class BorrowedBooksRestController {
private BorrowedBooksService borrowedBooksService;
    
    public BorrowedBooksRestController(BorrowedBooksService borrowedBooksService) {
        this.borrowedBooksService = borrowedBooksService;
    }
@PostMapping("/users")
    public void addBorrowedBooks(@RequestBody User theUser) {
                //this allows me to access the user id which is the foreign key for the borrowedBooks entity class but I need to access the borrowedBooks details as well such as bookName, genre etc. 
How do I send values from both user and borrowed books entity class.
   borrowedBooksService.save()//here we add the borrowedBook object to save in the database

    }

從上面的代碼可以看出,我可以從前端訪問 userId,但是我如何復制 hibernate 代碼中看到的 object 創建? 我需要從前端訪問 bookName 和流派以及用戶 ID。 我暫時使用 postman 作為 rest 客戶端。

對於您的任務,您需要前端的 userId 和 bookId 都傳遞給 addBorrowedBook controller。如果您有 bookId,因為 bookId 是圖書實體的主鍵,您可以唯一標識這本書。 無需知道書籍類型或名稱。 因此,從 bookId 中,您可以從書表中獲取數據並創建書 object。 然后,您可以將該書 object 添加到用戶 object 借書列表並保存/更新,如示例代碼中所示。

否則,如果您的表格不包含表格中的書籍詳細信息,則您沒有其他選擇,而不是從前端傳遞書籍類型等。

以這種方式傳遞書籍信息以及 userId。

    @postMapping('/user')
    public void addBorrowedBook(@RequestParam int userId, @RequestParam String genre, @RequestParam Stringbookname)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM