2017-02-08 23 views
2

我有一個一對多的關係,這兩個實體:與NetBeans產生爲什麼我的實體類的字段爲空?

public class Category implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "id") 
    private Short id; 
    @Basic(optional = false) 
    @Column(name = "name") 
    private String name; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "categoryId") 
    private Collection<Product> productCollection; 

    ... 

    @XmlTransient 
    public Collection<Product> getProductCollection() { 
     return productCollection; 
    } 

    ... 

public class Product implements Serializable { 

    ... 

    @JoinColumn(name = "category_id", referencedColumnName = "id") 
    @ManyToOne(optional = false) 
    private Category categoryId; 

    ... 

。問題是,當ControllerServlet調用getProductCollection()方法時,產品集合爲null。

@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 

    String userPath = request.getServletPath(); 
    Category selectedCategory; 
    Collection<Product> categoryProducts; 

    // if category page is requested 
    if (userPath.equals("/category")) { 
     // get categoryId from request 
     String categoryId = request.getQueryString(); 

     if (categoryId != null) { 

      // get selected category 
      selectedCategory = categoryFacade.find(Short.parseShort(categoryId)); 

      // place selected category in request scope 
      request.setAttribute("selectedCategory", selectedCategory); 

      // get all products for selected category 
      categoryProducts = selectedCategory.getProductCollection(); 

      // place category products in request scope 
      request.setAttribute("categoryProducts", categoryProducts); 
     } 

Notice the null value of productCollection when other fields has been yet initialized

編輯1:我聲明在ControllerServlet中的categoryFacade施加@EJB註解

public class ControllerServlet extends HttpServlet { 

    @EJB 
    private CategoryFacade categoryFacade; 

編輯2:這裏是文件的persistence.xml

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> 
    <persistence-unit name="AffableBeanPU" transaction-type="JTA"> 
     <jta-data-source>jdbc/affablebean</jta-data-source> 
     <properties/> 
    </persistence-unit> 
</persistence> 

編輯3:我正在使用TomEE 7.0.2

+0

如何在servlet中初始化'categoryFacade'? – perissf

+0

我用EJB註釋應用於categoryFacade的聲明 – ricber

+0

你在persistence.xml描述符中有'transaction-type =「JTA」嗎?您應該通過編輯將這些信息添加到問題中 – perissf

回答

-1

嘗試初始化集合空這樣的:即使有在延遲加載關係沒有結果

@OneToMany(cascade = CascadeType.ALL, mappedBy = "categoryId") 
private Collection<Product> productCollection = new HashSet<>(); 

那麼你將不會有空值。如果有加載的值,它們將被添加到集合中。

相關問題