Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8993121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:07:20+00:00 2026-06-15T23:07:20+00:00

I am writing a very basic library application in Java EE, just to understand

  • 0

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>
<%  } 
%>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:07:21+00:00Added an answer on June 15, 2026 at 11:07 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing this very basic J2SE application which alerts the user with some
I am writing a very basic website which involves the user to use a
I am writing a very basic web spider in java.I am facing one problem,
I'm writing an application in C# (it's very basic, for a friend of mine),
I found this open-source library that I want to use in my Java application.
I'm writing a very basic SDL application, but when I run it through valgrind,
As a hobby I have been writing a shell in java. It's very basic,
I have a very basic JavaScript question. I am writing a program which will
Problem Statement I'm writing a very basic WPF application to alter the contents of
I'm currently writing a very basic Java game based on the idea of Theme

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.