spring-data-jpa 使用Spring Boot的一对多单向关系

yb3bgrhw  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(206)

我 有 一 个 名 为 user 的 实体 类 , 代码 如下 :

@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

格式
下面 是 借阅 图书 实体 类 的 代码 :

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 支架 控制 器 实现 此 代码

@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 代码 中 看到 的 对象 创建 呢 ? 我 需要 从 前端 访问 bookName 和 genre 以及 用户 id 。 我 暂时 使用 postman 作为 休息 客户 端 。

wmtdaxz3

wmtdaxz31#

对于您任务,您需要从前端传递userId和bookId给addBorrowedBook控制器如果您有bookId,因为bookId是book实体的主键,所以可以唯一标识图书,不需要知道图书的类型和名称,所以从bookId可以从bookok表中取数据,创建book对象,然后将该book对象添加到user对象的借阅图书列表中保存/如示例代码所示更新。
否则,如果你的表中不包含图书详细信息,你没有其他选择,而不是从前端传递图书类型等。
要传递图书信息以及userId,请使用此方法。

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

相关问题