0

I have two tables into database: Book and Category. Now I want to make page where user can add books into Book table, but with selecting appropriate category from Category table.

I can add book into table but I can not save value category in Book table.

Book [Category[2]

As you can see category from Book table is Foreign key with category_id from Category table.

Here are model classes:

Book model

@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 && other.bookId != null) || (this.bookId != null && !this.bookId.equals(other.bookId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.biblioteka.app.domen.Book[ bookId=" + bookId + " ]";
}

}

Category model

 @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<Book> 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<Book> getBookCollection() {
    return bookCollection;
}

public void setBookCollection(Collection<Book> 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 && other.categoryId != null) || (this.categoryId != null && !this.categoryId.equals(other.categoryId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.biblioteka.app.domen.Category[ categoryId=" + categoryId + " ]";
}

}

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.

This is code from JSF addBook page.

  <p:layoutUnit position="center">
            <h:form>
                <p:inputText value="#{bookBean.name}" a:placeholder="Ime  knjige"></p:inputText><br/>
                <p:inputText value="#{bookBean.description}" a:placeholder="Opis knjige"></p:inputText><br/>
                <p:selectOneMenu value="#{bookBean.category}">
                    <f:selectItems value="#{categoryBean.allCategories}" var="c"
                                   itemLabel="#{c.name}" itemValue="#{c.categoryId}"/>

                </p:selectOneMenu>
                <b/><b/>
                <p:commandButton value="Dodaj knjigu" action="#{bookBean.addBook()}"/>
            </h:form>
        </p:layoutUnit>

As you can see I use selectOneMenu with value bookBean.category and then I am not sure what I need to set as value in selectItems.

This is BookBean 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<Book> 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;
}

}

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Zookey
  • 2,637
  • 13
  • 46
  • 80

1 Answers1

0

Try this :

 <f:selectItems value="#{categoryBean.allCategories}" var="c"
                               itemLabel="#{c.name}" itemValue="#{c}"/>

Listed item name would be category name and category will be assigned to bookBean.category and this can be set as book category and persisted.

Hope this helps.

Amal
  • 154
  • 4
  • I have tried this and somehow it does not work. Do you have any other idea? – Zookey Jan 15 '16 at 17:35
  • Next time, instead of saying "it does not work", google on the error message. The error message is the answer. It's just a matter of finding the explanation to it. You could have found http://stackoverflow.com/q/4734580 – BalusC Jan 15 '16 at 18:33
  • @BalusC Thanks, that helped. Now I get NPE since it seems that ManagedBean in Converter is allways null. How I can solve that? – Zookey Jan 15 '16 at 23:22