0

I am making a mini JSF project by using JPA about book store. There is a relationship with author and book. I mean a book could be written by multiple authors and an author can write books more than one.

I made(or tried to make) this relationship on java classes. But I am so confused about how to add a book with more than one writers. How to add them to database.

There are code parts that I have done so far. book.java :

public class Book {
    ...
@ManyToMany(cascade = CascadeType.ALL)
private List<Author> authorsOfBooks = new ArrayList<>();
    ...
 }

Writer.java:

public class Author {
    ...
    @ManyToMany(mappedBy = "authorsOfBooks")
    private List<Book> booksOfAuthor = new ArrayList<>();
    ...
    }

and books.xhtml:

 <p:panelGrid columns="2">   
 Book Name : <h:inputText value="#{BookBean.book.bookName}" />
 Author Names : <h:inputText value="#{BookBean.book.author}" />
 </p:panelGrid>

I am so confused about what should I do. Because "BookBean.book.author" is actually a list and I can not type and add data as a normal type.

berkancetin
  • 315
  • 1
  • 3
  • 18
  • Take a look at this answer: https://stackoverflow.com/questions/16417247 Does it help you? – Koray Tugay May 28 '19 at 23:05
  • will be helpful next step(when I show results), but my current problem is I can not save authors to a book – berkancetin May 28 '19 at 23:08
  • Add some sample data to your database and show them in UI, it may help you reveal how you can add more as well. You basically will need to the collection and persist back. – Koray Tugay May 28 '19 at 23:09
  • Do you mean adding by manually to database without my program? – berkancetin May 28 '19 at 23:11
  • I may not explain myself. I can add author and writer seperately, but can not make them in relation author_book table. My problem is that actually I think – berkancetin May 28 '19 at 23:16
  • https://stackoverflow.com/questions/52203892/updating-manytomany-relationships-in-jpa-or-hibernate/52207569#52207569 – K.Nicholas May 29 '19 at 02:37
  • 1
    Possible duplicate of [Updating ManyToMany relationships in JPA or Hibernate](https://stackoverflow.com/questions/52203892/updating-manytomany-relationships-in-jpa-or-hibernate) – K.Nicholas May 29 '19 at 02:38
  • No, my question was different. There is no duplication – berkancetin May 30 '19 at 20:39

0 Answers0