I am writing a very basic library application in Java EE, just to understand how it works. This application lets the user add a book, which is associated to a shelf. The association is bidirectional, many-to-one, so I would expect to be able to get the shelf which the book belongs to book.getShelf() and the books that a shelf contains shelf.getBooks().
Unfortunately, if I add a new Book to a Shelf, this Book is not returned by shelf.getBooks() until I redeploy my app. I would need your help to understand what I’m doing wrong.
Here is part of the code of the entities:
@Entity
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
protected String title;
protected String author;
@ManyToOne(fetch=FetchType.EAGER)
protected Shelf shelf;
//getters and setters follow
}
@Entity
public class Shelf implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(mappedBy = "shelf")
private List<Book> books;
protected String genre;
//getters and setters follow
}
The persistence of Book and Shelf is managed by the following stateless session bean, BookManager. It also contains the method to retrieve the list of books in a shelf (getBooksInShelf).
@Stateless
@LocalBean
public class BookManager{
@EJB
private ShelfFacade shelfFacade;
@EJB
private BookFacade bookFacade;
public List<Shelf> getShelves() {
return shelfFacade.findAll();
}
public List<Book> getBooksInShelf(Shelf shelf) {
return shelf.getBooks();
}
public void addBook(String title, String author, String shelf) {
Book b = new Book();
b.setName(title);
b.setAuthor(author);
b.setShelf(getShelfFromGenre(shelf));
bookFacade.create(b);
}
//if there is a shelf of the genre "shelf", return it
//otherwise, create a new shelf and persist it
private Shelf getShelfFromGenre(String shelf) {
List<Shelf> shelves = shelfFacade.findAll();
for (Shelf s: shelves){
if (s.getGenre().equals(shelf)) return s;
}
Shelf s = new Shelf();
s.setGenre(shelf);
shelfFacade.create(s);
return s;
}
public int numberOfBooks(){
return bookFacade.count();
}
}
In the JSP: (I’m writing only the portion of code for book presentation)
<jsp:useBean id="bookManager" class="sessionBean.BookManager" scope="request"/>
// ...
<% List<Book> books;
for(Shelf s: shelves){
books = bookManager.getBooksInShelf(s);
%>
<h2><%= s.getGenre() %></h2>
<ul>
<% if (books.size()==0){
%> <p>The shelf is empty.</p>
<% }
for (Book b: books){
%> <li> <em><%= b.getAuthor()%></em>, <%= b.getName() %> </li>
<% }
%> </ul>
<% }
%>
You must maintain bi-directional relationships. When you create a new Book and set its Shelf, you must add the Book to the Shelf’s books.