这是我的第一篇文章,如果不能理解,请随时提问。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;
}
}
......严重吗?尝试减少问题的大小以获得更少的提议,甚至可以得到答案。 –
对不起,我尝试通过移动下面的代码并删除一些代码来使问题更具可读性。现在的第一行应该显示我的问题。 – Norman