簡體   English   中英

Spring Boot JPA /休眠表連接

[英]Spring boot jpa/hibernate tables connection

專業人士!

我有個問題。 在Spring Boot中通過ID連接2個表的更好的主意是什么? 例如:我們有2個表client和book。 每個客戶都可以借書,並且書的狀態將更改。

我知道如何使用SQL在數據庫中創建它。 但是問題是,如何在jpa / hibernate中執行此操作。

我有一個ManyToMany或OneToMany的錯誤.....

@Entity 
public class Book implements java.io.Serializable {

private static final long serialVersionUID = 1L;

@TableGenerator(name = "BOOK_GEN", allocationSize = 1)
@Id
@GeneratedValue(generator = "BOOK_GEN")
private int id;

private String book_name;
private String ISBN;
private String publish_year;
private String publisher;
private Boolean status;

@OneToMany(mappedBy="book" ,cascade=CascadeType.ALL , fetch = FetchType.LAZY)
private Collection<Client>  authors =new ArrayList<Client>();

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getBook_name() {
    return book_name;
}

public void setBook_name(String book_name) {
    this.book_name = book_name;
}

public String getISBN() {
    return ISBN;
}

public void setISBN(String iSBN) {
    ISBN = iSBN;
}

public String getPublish_year() {
    return publish_year;
}

public void setPublish_year(String publish_year) {
    this.publish_year = publish_year;
}

public String getPublisher() {
    return publisher;
}

public void setPublisher(String publisher) {
    this.publisher = publisher;
}

public Boolean getStatus() {
    return status;
}

public void setStatus(Boolean status) {
    this.status = status;
}

public Collection<Author> getAuthors() {
    return authors;
}

public void setClients(Collection<Client> clients) {
    this.clients = clients;
}}




@Entity 
public class Client implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @TableGenerator(name = "CLT_GEN", allocationSize = 1)
    @Id
    @GeneratedValue(generator = "CLT_GEN")
    private int id;

    private Boolean bookedstatus;
    private Boolean bookstatus;

    @ManyToOne(fetch = FetchType.LAZY)
    private Book book;

    private String name ;
    private String surname;



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Boolean getBookedstatus() {
        return bookedstatus;
    }

    public void setBookedstatus(Boolean bookedstatus) {
        this.bookedstatus = bookedstatus;
    }

    public Boolean getBookstatus() {
        return bookstatus;
    }

    public void setBookstatus(Boolean bookstatus) {
        this.bookstatus = bookstatus;
    }
}

由於您已經提到“客戶可以借書”,因此一對一映射應該最適合您。 請參考下面的代碼。 另外,您也沒有提到當前實施中遇到的錯誤。

@Entity
public class Book implements java.io.Serializable {

private static final long serialVersionUID = 1L;

@TableGenerator(name = "BOOK_GEN", allocationSize = 1)
@Id
@GeneratedValue(generator = "BOOK_GEN")
private int id;

private String book_name;
private String ISBN;
private String publish_year;
private String publisher;
private Boolean status;

@OneToOne(fetch = FetchType.LAZY, mappedBy = "book", cascade = CascadeType.ALL)
private Collection<Client> authors;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getBook_name() {
    return book_name;
}

public void setBook_name(String book_name) {
    this.book_name = book_name;
}

public String getISBN() {
    return ISBN;
}

public void setISBN(String iSBN) {
    ISBN = iSBN;
}

public String getPublish_year() {
    return publish_year;
}

public void setPublish_year(String publish_year) {
    this.publish_year = publish_year;
}

public String getPublisher() {
    return publisher;
}

public void setPublisher(String publisher) {
    this.publisher = publisher;
}

public Boolean getStatus() {
    return status;
}

public void setStatus(Boolean status) {
    this.status = status;
}

public Collection<Client> getAuthors() {
    return authors;
}

public void setAuthors(Collection<Client> authors) {
    this.authors = authors;
}

}
    @Entity 
public class Client implements java.io.Serializable {

private static final long serialVersionUID = 1L;

@TableGenerator(name = "CLT_GEN", allocationSize = 1)
@Id
@GeneratedValue(generator = "CLT_GEN")
private int id;

private Boolean bookedstatus;
private Boolean bookstatus;

@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private Book book;

private String name ;
private String surname;



public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Book getBook() {
    return book;
}

public void setBook(Book book) {
    this.book = book;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public Boolean getBookedstatus() {
    return bookedstatus;
}

public void setBookedstatus(Boolean bookedstatus) {
    this.bookedstatus = bookedstatus;
}

public Boolean getBookstatus() {
    return bookstatus;
}

public void setBookstatus(Boolean bookstatus) {
    this.bookstatus = bookstatus;
}
}

暫無
暫無

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

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