微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在ORM模型的Entity类中表示外键列并将其用于在数据库中保存数据

如何解决如何在ORM模型的Entity类中表示外键列并将其用于在数据库中保存数据

请考虑以下技术堆栈:

还要考虑2个表: 书籍和作者


Books:

book_id : Integer & PrimaryKey

author_id: Integer & ForeignKey to author_id column of Authors table

book_title: Varchar

date: Date

Authors:

author_id : Integer & PrimaryKey

author_name: Varchar

date: Date

======== 问题陈述: 我需要设计2个休息服务端点:

  1. 在Authors表中添加Author。
  2. 将书添加到“书”表中。

问题:直接添加作者记录。

  1. 在表中添加Book记录的最佳做法是什么?
  2. 我们需要在“图书的请求正文” JSON中添加作者信息吗?
  3. 我们可以限制用户提供仅在authors表中存在的作者吗?

代码部分

图书实体:

@Entity
@Table(name = "books")
public class Books {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "book_id",nullable = false)
    private Integer book_id;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "author_id",referencedColumnName = "author_id")
    private Authors author_id;

    @Column(name = "book_title",nullable = false)
    private String book_title;
    @Column(name = "created_date",nullable = false)
    private Date created_date;

    public Integer getBook_id() {
        return book_id;
    }

    public void setBook_id(Integer book_id) {
        this.book_id = book_id;
    }

    public Authors getAuthor_id() {
        return author_id;
    }

    public void setAuthor_id(Authors author_id) {
        this.author_id = author_id;
    }

    public String getBook_title() {
        return book_title;
    }

    public void setBook_title(String book_title) {
        this.book_title = book_title;
    }


    public Date getCreated_date() {
        return created_date;
    }

    public void setCreated_date(Date created_date) {
        this.created_date = created_date;
    }

}

作者身份:

@Entity
@Table(name = "authors")
public class Authors {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "author_id",nullable = false)
    private Integer author_id;
    @Column(name = "author_name",nullable = false)
    private String author_name;
    @Column(name = "created_date",nullable = false)
    private Date created_date;

    public Integer getAuthor_id() {
        return author_id;
    }

    public void setAuthor_id(Integer author_id) {
        this.author_id = author_id;
    }

    public String getAuthor_name() {
        return author_name;
    }

    public void setAuthor_name(String author_name) {
        this.author_name = author_name;
    }


    public Date getCreated_date() {
        return created_date;
    }

    public void setCreated_date(Date created_date) {
        this.created_date = created_date;
    }
}

说明:

@Service
public class LibraryService {

    @Autowired
    private AuthorDao authorDao;

    @Autowired
    private BooksDao booksDao;

    public void addAuthor(Authors authors){
        authors.setCreated_date(new Date());
        authors.setLast_modified_date(new Date());
        authorDao.save(authors);
    }


    public void addBooks(Books books){
        books.setCreated_date(new Date());
        books.setLast_modified_date(new Date());

        booksDao.save(books);
    }

}

控制器类:

@RestController
public class LibraryController {

    @Autowired
    private LibraryService libraryService;

    @PostMapping(value = "/author",produces = MediaType.APPLICATION_JSON_VALUE)
    public void addProduct(@RequestBody Authors authors) throws GlobalException {
        try{
            libraryService.addAuthor(authors);
        }catch(RuntimeException exception){
            throw new GlobalException(exception.getLocalizedMessage());
        }

    }


    @PostMapping(value = "/book",produces = MediaType.APPLICATION_JSON_VALUE)
    public void addBook(@RequestBody Books books) throws GlobalException {
        try{
            libraryService.addBooks(books);
        }catch(RuntimeException exception){
            throw new GlobalException(exception.getLocalizedMessage());
        }

    }


}

如果在添加Book时以以下格式给出JSON(这意味着触发addBooks方法),则新记录将成功插入到Authors ad Books表中。但这是一个好习惯吗?如果作者表中已经存在作者姓名,该怎么办。如何仅在请求正文json中发送author_id,以便成功触发?

{
    "author_id": {
        "author_name": "Chetan Bhagat","created_date": "2020-11-03",},"book_title": "Two states",}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。