EJB3和JSF中多对一关系中的select语句问题

时间:2010-10-23 08:36:10

标签: jsf jpa

大家好 我想知道如何选择多对一关系 我有两个表Sub_category和Items 子类别是关系的,它包含项目列表

两节课:

@Entity
@Table(name = "item")
@NamedQueries({
    @NamedQuery(name = "Items.findAll", query = "SELECT i FROM Items i"),
    @NamedQuery(name = "Items.findByItemid", query = "SELECT i FROM Items i WHERE i.itemid = :itemid"),
    @NamedQuery(name = "Items.findByItemName", query = "SELECT i FROM Items i WHERE i.itemName = :itemName"),
    @NamedQuery(name = "Items.findByItemDescribe", query = "SELECT i FROM Items i WHERE i.itemDescribe = :itemDescribe"),
    @NamedQuery(name = "Items.findByImg", query = "SELECT i FROM Items i WHERE i.img = :img"),
    @NamedQuery(name = "Items.findByInstock", query = "SELECT i FROM Items i WHERE i.instock = :instock"),
    @NamedQuery(name = "Items.findByPrice", query = "SELECT i FROM Items i WHERE i.price = :price"),
    @NamedQuery(name = "Items.findByFine", query = "SELECT i FROM Items i WHERE i.fine = :fine"),
    @NamedQuery(name = "Items.findByDateexp", query = "SELECT i FROM Items i WHERE i.dateexp = :dateexp"),
    @NamedQuery(name = "Items.findByAuthor", query = "SELECT i FROM Items i WHERE i.author = :author"),
    @NamedQuery(name = "Items.findByToprent", query = "SELECT i FROM Items i WHERE i.toprent = :toprent"),
    @NamedQuery(name = "Items.findByStatus", query = "SELECT i FROM Items i WHERE i.status = :status")})
public class Items implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "itemid")
    private Integer itemid;
    @Basic(optional = false)
    @Column(name = "item_name")
    private String itemName;
    @Column(name = "item_describe")
    private String itemDescribe;
    @Lob
    @Column(name = "item_detail")
    private String itemDetail;
    @Column(name = "img")
    private String img;
    @Basic(optional = false)
    @Column(name = "instock")
    private int instock;
    @Basic(optional = false)
    @Column(name = "price")
    private BigDecimal price;
    @Basic(optional = false)
    @Column(name = "fine")
    private BigDecimal fine;
    @Basic(optional = false)
    @Column(name = "dateexp")
    private int dateexp;
    @Column(name = "author")
    private String author;
    @Column(name = "toprent")
    private Integer toprent;
    @Column(name = "status")
    @Enumerated(EnumType.STRING)
    private ItemStatus status;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "item")
    private List<RentItem> rentItemList;
    @JoinColumn(name = "cat_id", referencedColumnName = "subcatid")
    @ManyToOne(optional = false)
    private SubCat subCat;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "item")
    private List<Cart> cartList;

    public Items() {
    }

    public Items(Integer itemid) {
        this.itemid = itemid;
    }

    public Items(Integer itemid, String itemName, int instock, BigDecimal price, BigDecimal fine, int dateexp) {
        this.itemid = itemid;
        this.itemName = itemName;
        this.instock = instock;
        this.price = price;
        this.fine = fine;
        this.dateexp = dateexp;
    }

    public Integer getItemid() {
        return itemid;
    }

    public void setItemid(Integer itemid) {
        this.itemid = itemid;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemDescribe() {
        return itemDescribe;
    }

    public void setItemDescribe(String itemDescribe) {
        this.itemDescribe = itemDescribe;
    }

    public String getItemDetail() {
        return itemDetail;
    }

    public void setItemDetail(String itemDetail) {
        this.itemDetail = itemDetail;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public int getInstock() {
        return instock;
    }

    public void setInstock(int instock) {
        this.instock = instock;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getFine() {
        return fine;
    }

    public void setFine(BigDecimal fine) {
        this.fine = fine;
    }

    public int getDateexp() {
        return dateexp;
    }

    public void setDateexp(int dateexp) {
        this.dateexp = dateexp;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Integer getToprent() {
        return toprent;
    }

    public void setToprent(Integer toprent) {
        this.toprent = toprent;
    }

    public ItemStatus getStatus() {
        return status;
    }

    public void setStatus(ItemStatus status) {
        this.status = status;
    }

    public List<RentItem> getRentItemList() {
        return rentItemList;
    }

    public void setRentItemList(List<RentItem> rentItemList) {
        this.rentItemList = rentItemList;
    }

    public SubCat getSubCat() {
        return subCat;
    }

    public void setSubCat(SubCat subCat) {
        this.subCat = subCat;
    }

    public List<Cart> getCartList() {
        return cartList;
    }

    public void setCartList(List<Cart> cartList) {
        this.cartList = cartList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (itemid != null ? itemid.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 Items)) {
            return false;
        }
        Items other = (Items) object;
        if ((this.itemid == null && other.itemid != null) || (this.itemid != null && !this.itemid.equals(other.itemid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.entity.Item[itemid=" + itemid + "]";
    }

}

和子类别:

@Entity
@Table(name = "sub_cat")
@NamedQueries({
    @NamedQuery(name = "SubCat.findAll", query = "SELECT s FROM SubCat s"),
    @NamedQuery(name = "SubCat.findBySubcatid", query = "SELECT s FROM SubCat s WHERE s.subcatid = :subcatid"),
    @NamedQuery(name = "SubCat.findBySubcatName", query = "SELECT s FROM SubCat s WHERE s.subcatName = :subcatName")})
public class SubCat implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "subcatid")
    private Integer subcatid;
    @Basic(optional = false)
    @Column(name = "subcat_name")
    private String subcatName;
    @JoinColumn(name = "cat_parent", referencedColumnName = "cate_id")
    @ManyToOne(optional = false)
    private Category category;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "subCat")
    private List<Items> itemList;

    public SubCat() {
    }

    public SubCat(Integer subcatid) {
        this.subcatid = subcatid;
    }

    public SubCat(Integer subcatid, String subcatName) {
        this.subcatid = subcatid;
        this.subcatName = subcatName;
    }

    public Integer getSubcatid() {
        return subcatid;
    }

    public void setSubcatid(Integer subcatid) {
        this.subcatid = subcatid;
    }

    public String getSubcatName() {
        return subcatName;
    }

    public void setSubcatName(String subcatName) {
        this.subcatName = subcatName;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public List<Items> getItemList() {
        return itemList;
    }

    public void setItemList(List<Items> itemList) {
        this.itemList = itemList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (subcatid != null ? subcatid.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 SubCat)) {
            return false;
        }
        SubCat other = (SubCat) object;
        if ((this.subcatid == null && other.subcatid != null) || (this.subcatid != null && !this.subcatid.equals(other.subcatid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.entity.SubCat[subcatid=" + subcatid + "]";
    }

}

我有句柄subcat的无状态bean,例如:

@Stateless
@LocalBean
public class SubCatDAO {
    @PersistenceContext(unitName = "mcGrawLibPro-ejbPU")
    private EntityManager em;


    public List<SubCat> retrieveAllSubCat(){
        return em.createNamedQuery("SubCat.findAll").getResultList();
    }

    public SubCat updateSubCat(SubCat sc){
        return em.merge(sc);
    }

    public void deleteSubCat(SubCat sc){
        em.remove(em.merge(sc));
    }

    public SubCat addSubCat(SubCat sc){
        em.persist(sc);
        return sc;
    }

    public void persist(Object object) {
        em.persist(object);
    }

     public List<Category> retrieveAllCat(){
        return em.createNamedQuery("Category.findAll").getResultList();
    }


    public List<Items> getAllItemsSubCat(SubCat sub){

        em.refresh(em.merge(sub));
        List<Items> items = sub.getItemList();
        ArrayList<Items> toReturn =  new ArrayList<Items>(items.size());
        for(Items iItem : items){
            toReturn.add(iItem);
        }
        return toReturn;
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

}

正如你在subcat的无状态bean中看到的那样,我写了一个方法返回List 并在JSF Managed Bean of subcat中编写一个方法返回List to view(JSF)

如:

  public List<Items> getAllItemsSub(){

    return subCatDAO.getAllItemsSubCat(sub);
}

(subCatDAO是无状态bean)

也在subcat i inital subcat的JSF Managened Bean中跟随:

public BeanConstructor(){
  sub = new SubCat(1);
}

我的问题是在视图中(JSF)我是向用户显示的项目的打印列表,但我无法得到任何东西,我只是看到空白, 我的代码示例:

<h:ouputText value="#{bean.allItemSub.itemid}"/>

当我打印bean.allItemSub时,它返回[]&lt; === 为什么空了?

1 个答案:

答案 0 :(得分:1)

我真的不明白你的EJB中getAllItemsSubCat(SubCat sub)方法的实现。我会像这样重写它。

首先,添加一个命名查询以查找给定Item的{​​{1}}:

SubCategory

并重写EJB方法如下:

@NamedQuery(name = "Items.findBySubCat", 
            query = "SELECT i FROM Items i WHERE i.subCat = :subCat")

然后,激活SQL日志记录(在JPA提供程序级别)以确保该方法实际返回一些内容。