title: Get JSF Drop down value and save to database
body: <p>I have two tables into database: <strong>Book</strong> and <strong>Category</strong>. Now I want to make page where user can add books into Book table, but with selecting appropriate category from Category table.</p>
  
  <p>I can add <strong>book</strong> into table but I can not save value <strong>category</strong> in <strong>Book</strong> table. </p>
  
  <p><a href="https://i.stack.imgur.com/Y3GG0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y3GG0.png" alt="Book"></a>
  [<img src="https://i.stack.imgur.com/Dqdj9.png" alt="Category[2]"></p>
  
  <p>As you can see <strong>category</strong> from <strong>Book</strong> table is <strong>Foreign key</strong> with <strong>category_id</strong> from <strong>Category</strong> table.</p>
  
  <p>Here are model classes:</p>
  
  <p><strong>Book model</strong></p>
  
  <pre><code>@Entity
  @Table(name = "book")
  @XmlRootElement
  @NamedQueries({
  @NamedQuery(name = "Book.findAll", query = "SELECT b FROM Book b"),
  @NamedQuery(name = "Book.findByBookId", query = "SELECT b FROM Book b WHERE b.bookId = :bookId")})
  
  public class Book implements Serializable {
  
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "book_id")
  private Integer bookId;
  @Basic(optional = false)
  @NotNull
  @Lob
  @Size(min = 1, max = 65535)
  @Column(name = "name")
  private String name;
  @Lob
  @Size(max = 65535)
  @Column(name = "description")
  private String description;
  @JoinColumn(name = "category", referencedColumnName = "category_id")
  @ManyToOne
  private Category category;
  
  public Book() {
  }
  
  public Book(Integer bookId) {
      this.bookId = bookId;
  }
  
  public Book(Integer bookId, String name) {
      this.bookId = bookId;
      this.name = name;
  }
  
  public Integer getBookId() {
      return bookId;
  }
  
  public void setBookId(Integer bookId) {
      this.bookId = bookId;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getDescription() {
      return description;
  }
  
  public void setDescription(String description) {
      this.description = description;
  }
  
  public Category getCategory() {
      return category;
  }
  
  public void setCategory(Category category) {
      this.category = category;
  }
  
  @Override
  public int hashCode() {
      int hash = 0;
      hash += (bookId != null ? bookId.hashCode() : 0);
      return hash;
  }
  
  @Override
  public boolean equals(Object object) {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Book)) {
          return false;
      }
      Book other = (Book) object;
      if ((this.bookId == null &amp;&amp; other.bookId != null) || (this.bookId != null &amp;&amp; !this.bookId.equals(other.bookId))) {
          return false;
      }
      return true;
  }
  
  @Override
  public String toString() {
      return "com.biblioteka.app.domen.Book[ bookId=" + bookId + " ]";
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Category model</strong></p>
  
  <pre><code> @Entity
   @Table(name = "category")
   @XmlRootElement
   @NamedQueries({
  @NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c"),
  @NamedQuery(name = "Category.findByCategoryId", query = "SELECT c FROM Category c WHERE c.categoryId = :categoryId")})
  
   public class Category implements Serializable {
  
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "category_id")
  private Integer categoryId;
  @Basic(optional = false)
  @NotNull
  @Lob
  @Size(min = 1, max = 65535)
  @Column(name = "name")
  private String name;
  @Lob
  @Size(max = 65535)
  @Column(name = "description")
  private String description;
  @OneToMany(mappedBy = "category")
  private Collection&lt;Book&gt; bookCollection;
  
  public Category() {
  }
  
  public Category(Integer categoryId) {
      this.categoryId = categoryId;
  }
  
  public Category(Integer categoryId, String name) {
      this.categoryId = categoryId;
      this.name = name;
  }
  
  public Integer getCategoryId() {
      return categoryId;
  }
  
  public void setCategoryId(Integer categoryId) {
      this.categoryId = categoryId;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getDescription() {
      return description;
  }
  
  public void setDescription(String description) {
      this.description = description;
  }
  
  @XmlTransient
  public Collection&lt;Book&gt; getBookCollection() {
      return bookCollection;
  }
  
  public void setBookCollection(Collection&lt;Book&gt; bookCollection) {
      this.bookCollection = bookCollection;
  }
  
  @Override
  public int hashCode() {
      int hash = 0;
      hash += (categoryId != null ? categoryId.hashCode() : 0);
      return hash;
  }
  
  @Override
  public boolean equals(Object object) {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Category)) {
          return false;
      }
      Category other = (Category) object;
      if ((this.categoryId == null &amp;&amp; other.categoryId != null) || (this.categoryId != null &amp;&amp; !this.categoryId.equals(other.categoryId))) {
          return false;
      }
      return true;
  }
  
  @Override
  public String toString() {
      return "com.biblioteka.app.domen.Category[ categoryId=" + categoryId + " ]";
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Now I have JSF page where I add bookes to database. I have dropdown lists that loads categories into it. User should select one category and save book to table.</p>
  
  <p>This is code from <strong>JSF addBook page</strong>.</p>
  
  <pre><code>  &lt;p:layoutUnit position="center"&gt;
              &lt;h:form&gt;
                  &lt;p:inputText value="#{bookBean.name}" a:placeholder="Ime  knjige"&gt;&lt;/p:inputText&gt;&lt;br/&gt;
                  &lt;p:inputText value="#{bookBean.description}" a:placeholder="Opis knjige"&gt;&lt;/p:inputText&gt;&lt;br/&gt;
                  &lt;p:selectOneMenu value="#{bookBean.category}"&gt;
                      &lt;f:selectItems value="#{categoryBean.allCategories}" var="c"
                                     itemLabel="#{c.name}" itemValue="#{c.categoryId}"/&gt;
  
                  &lt;/p:selectOneMenu&gt;
                  &lt;b/&gt;&lt;b/&gt;
                  &lt;p:commandButton value="Dodaj knjigu" action="#{bookBean.addBook()}"/&gt;
              &lt;/h:form&gt;
          &lt;/p:layoutUnit&gt;
  </code></pre>
  
  <p>As you can see I use <strong>selectOneMenu</strong> with value <strong>bookBean.category</strong> and then I am not sure what I need to set as value in <strong>selectItems</strong>.</p>
  
  <p>This is <strong>BookBean</strong> code:</p>
  
  <pre><code> @ManagedBean
   @ApplicationScoped
   public class BookBean {
  
  String name;
  String description;
  int categoryId;
  Category category;
  
  @Inject
  public BookEJB bookEJB;
  
  public void addBook(){
  
      Book book = new Book();
      book.setName(name);
      book.setDescription(description);
      book.setCategory(category);
  
      bookEJB.addBook(book);
  }
  
  public List&lt;Book&gt; getAllBooks(){
      return bookEJB.getAll();
  }
  
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getDescription() {
      return description;
  }
  
  public void setDescription(String description) {
      this.description = description;
  }
  
  public int getCategoryId() {
      return categoryId;
  }
  
  public void setCategoryId(int categoryId) {
      this.categoryId = categoryId;
  }
  
  public Category getCategory() {
      return category;
  }
  
  public void setCategory(Category category) {
      this.category = category;
  }
  
  public BookEJB getBookEJB() {
      return bookEJB;
  }
  
  public void setBookEJB(BookEJB bookEJB) {
      this.bookEJB = bookEJB;
  }
  </code></pre>
  
  <p>}</p>