简体   繁体   中英

Spring boot jpa/hibernate tables connection

professionals!

I have a question. What is a better idea to connect 2 tables in spring boot via ID? As an example: we have 2 tables client and books. Each client can borrow a book and the status of book will be changed.

I know how to make it in DB using SQL. But the question is, how to do this in jpa/hibernate.

I have an error of ManyToMany or 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;
    }
}

Since you have mentioned "A client can borrow a book", one-to-one mapping should work the best for you. Please refer to the below code. Also you didn't mention the error you are facing with your current implementation.

@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;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM