2017-02-08 6 views
2

は、私は多くの関係に一つにこれら2つのエンティティがありますなぜ私のエンティティクラスのフィールドはnullですか?

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; 

    ... 

は、NetBeansで生成されました。問題は、getProductCollection()メソッドがControllerServletによって呼び出されたとき、Collection of Productが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:私は編集2

public class ControllerServlet extends HttpServlet { 

    @EJB 
    private CategoryFacade categoryFacade; 

@EJB注釈を適用するControllerServletのでcategoryFacadeを宣言:ここでは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

'categoryFacade'はサーブレットでどのように初期化されていますか? – perissf

+0

categoryFacadeの宣言に適用されたEJBアノテーションを使用しました – ricber

+0

persistence.xml記述子に 'transaction-type =" JTA "'がありますか? – perissf

答えて

-1

のような空のコレクションを初期化するために試してみてください。そして、あなたは何の結果が遅延ロードされた関係に存在しない場合でも、null値を持ちません

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

。読み込まれた値がある場合、それらはコレクションに追加されます。

関連する問題