获取JSF下拉列表值并保存到数据库

获取JSF下拉列表值并保存到数据库,jsf,jsf-2,selectonemenu,Jsf,Jsf 2,Selectonemenu,数据库中有两个表:Book和Category。现在我想创建一个页面,用户可以在其中向图书表中添加图书,但需要从分类表中选择合适的分类 我可以将书籍添加到表中,但无法将值类别保存到书籍表中 [ 正如您可以从图书中看到的类别表是外键和类别id来自类别表 以下是模型类: 图书型号 @Entity @Table(name = "book") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Book.findAll", query = "SELEC

数据库中有两个表:BookCategory。现在我想创建一个页面,用户可以在其中向图书表中添加图书,但需要从分类表中选择合适的分类

我可以将书籍添加到表中,但无法将值类别保存到书籍表中

[

正如您可以从图书中看到的类别表是外键类别id来自类别

以下是模型类:

图书型号

@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 + " ]";
}
 @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 + " ]";
}
}

类别模型

@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 + " ]";
}
 @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 + " ]";
}
@实体
@表(name=“category”)
@XmlRootElement
@命名查询({
@NamedQuery(name=“Category.findAll”,query=“从类别c中选择c”),
@NamedQuery(name=“Category.findByCategoryId”,query=“从c类中选择c,其中c.categoryId=:categoryId”))
公共类类别实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@基本(可选=假)
@列(name=“category\u id”)
私有整数类别ID;
@基本(可选=假)
@NotNull
@高球
@尺寸(最小值=1,最大值=65535)
@列(name=“name”)
私有字符串名称;
@高球
@尺寸(最大值=65535)
@列(name=“description”)
私有字符串描述;
@OneToMany(mappedBy=“类别”)
私人藏书;
公共类别(){
}
公共类别(整数类别ID){
this.categoryId=categoryId;
}
公共类别(整数类别ID,字符串名称){
this.categoryId=categoryId;
this.name=名称;
}
公共整数getCategoryId(){
返回类别ID;
}
public void setCategoryId(整型categoryId){
this.categoryId=categoryId;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
@XmlTransient
公共集合getBookCollection(){
还书回收;
}
公共作废setBookCollection(集合账簿集合){
this.bookCollection=bookCollection;
}
@凌驾
公共int hashCode(){
int hash=0;
hash+=(categoryId!=null?categoryId.hashCode():0);
返回散列;
}
@凌驾
公共布尔等于(对象){
//TODO:警告-如果未设置id字段,此方法将不起作用
如果(!(类别的对象实例)){
返回false;
}
类别其他=(类别)对象;
如果((this.categoryId==null&&other.categoryId!=null)| |(this.categoryId!=null&&other.categoryId.equals(other.categoryId))){
返回false;
}
返回true;
}
@凌驾
公共字符串toString(){
返回“com.biblioteka.app.domen.Category[categoryId=“+categoryId+””;
}
}

现在我有了JSF页面,在这里我将书籍添加到数据库中。我有下拉列表,可以将类别加载到其中。用户应该选择一个类别并将书籍保存到表中

这是来自JSF addBook页面的代码

  <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>



如您所见,我使用带有值的selectOneMenubookBean.category
,然后我不确定需要在selectItems中设置哪些值

  <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>
这是BookBean代码:

 @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;
}
@ManagedBean
@适用范围
公共类书豆{
字符串名;
字符串描述;
int分类;
类别;
@注入
公共图书;
公共无效地址簿(){
书=新书();
book.setName(名称);
书.集说明(说明);
图书分类(分类);
bookEJB.addBook(book);
}
公共列表getAllBooks(){
返回bookEJB.getAll();
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
public int getCategoryId(){
返回类别ID;
}
public void setCategoryId(int categoryId){
this.categoryId=categoryId;
}
公共类别getCategory(){
退货类别;
}
公共无效集合类别(类别){
this.category=类别;
}
public BookEJB getBookEJB(){
返回bookEJB;
}
公共无效设置BookEJB(BookEJB BookEJB){
this.bookEJB=bookEJB;
}
}试试这个:

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

列出的项目名称将是类别名称,类别将分配给bookBean.category,这可以设置为book category并持久化


希望这能有所帮助。

我试过这个,但不知怎么的它不起作用。你还有其他想法吗?下次,不要说“它不起作用”,谷歌在错误消息上。错误消息就是答案。这只是找到解释的问题。你可以找到@BalusC谢谢,这很有帮助。现在我得到了NPE,因为转换器中的ManagedBean似乎总是空的。我如何解决这个问题?