2011-09-22 41 views
-5

這是我的第一篇文章,如果不能理解,請隨時提問。jboss 6.1.0.final gwt和ejb組合

我想開發一個gwt應用程序,它使用我自己的ejb類從外部商店項目。

我的ServiceImpl根據需要到達ejb,但是我不能像我這樣使用注入。在我的ejb-class中,我調用一個函數來創建帶有虛擬數據的測試數據庫。對於這個(以及後來的任何請求當然)我想注入一個EntityManager。這是在我的Base-Class HomeBase中完成的。 問題: entityManager可能未被初始化。我tryed這兩個註解:

@PersistenceUnit(unitName = "sung.app.kylintv.ejb") 
protected EntityManagerFactory entityManagerFactory; 

protected EntityManager entityManager = entityManagerFactory.createEntityManager(); 

爲好,如:

@PersistenceContext(unitName="sung.app.kylintv.ejb") 
protected EntityManager entityManager; 

我運行JBoss 6.1.0.Final,GWT 2.4服務器端調用我的EJB功能。 JBoss啓動正確,沒有顯示任何錯誤。然而,在調用函數時出現此錯誤消息:

Caused by: javax.ejb.EJBException: java.lang.NullPointerException 
... 
Caused by: java.lang.NullPointerException 
at sung.app.kylintv.HomeBase.<init>(HomeBase.java:28) [:] 
at sung.app.kylintv.product.ProductHome.<init>(ProductHome.java:35) [:] 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [:1.6.0_25] 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [:1.6.0_25] 
at 

通過調試,我發現了EntityManager爲NULL功能。 我怎樣才能讓注射工作?或者我對此採取了完全錯誤的做法?

對於更詳細的信息,如果需要: 代碼:

package sung.app.kylintv.gwt.server; 

import javax.ejb.EJB; 

import sung.app.kylintv.gwt.client.DatabaseBuilderService; 
import sung.app.kylintv.product.Product; 
import sung.app.kylintv.product.ProductHome; 

import com.google.gwt.user.server.rpc.RemoteServiceServlet; 

public class DatabaseBuilderServiceImpl extends RemoteServiceServlet implements DatabaseBuilderService 
{ 
@EJB(mappedName = "sung/app/kylintv/product") 
private transient Product product; 


@Override 
public boolean createDefaultDatabaseEntries() 
{ 
    return product.createTestEntry(); 
} 
} 

啓動的類(通過接口產品)產品展示首頁:

@Stateless(name="Product") 
@Local(Product.class) 
public class ProductHome extends HomeBase<ProductEntity> implements Serializable, Product 
{ 
@EJB 
protected Option sessionOption; 

@TransactionAttribute(REQUIRED) 
public boolean createTestEntry() 
{ 
    try 
    { 
     System.out.println("TEST creating Data BEGIN"); 

     ProductEntity currentProduct = new ProductEntity(); 

// ++++ fill FIRST product ++++ 
     currentProduct.setName("bo_product_europe_basic_name"); 
     currentProduct.setDescription("bo_product_europe_basic_description"); 

     getEntityManager().persist(currentProduct); **<- HERE THE ERROR OCCURS** 

     ... **For better overview I removed the further object-creation** 
    } 
    catch(Exception e) 
    { 
     //print out an error message and return false 
     System.out.println(e.getCause().getMessage()); 
     return false; 
    } 
    return true; 
} 

} 

擴展HomeBase可:

public abstract class HomeBase<T> 
{ 
@PersistenceUnit(unitName = "sung.app.kylintv.ejb") 
protected EntityManagerFactory entityManagerFactory; 

private EntityManager entityManager = entityManagerFactory.createEntityManager(); 


// @PersistenceContext(unitName = "sung_app_kylintv") 
// protected EntityManager entityManager; 
//  
public void setEntityManager(EntityManager entityManager) { 
    this.entityManager = entityManager; 
} 


public EntityManager getEntityManager() { 
    return entityManager; 
} 
} 
+2

......嚴重嗎?嘗試減少問題的大小以獲得更少的提議,甚至可以得到答案。 –

+0

對不起,我嘗試通過移動下面的代碼並刪除一些代碼來使問題更具可讀性。現在的第一行應該顯示我的問題。 – Norman

回答

0

它看起來像你試圖設置一個EntityManagerentityManagerFactory,我沒有看到任何地方初始化。也許你應該檢查看看受保護的EntityManagerFactory entityManagerFactory是否爲空。如果是這樣,那就是你的問題。

+0

感謝您的幫助!你是完全正確的。該課程已啓動,但工廠未初始化。 – Norman