jboss 6.1.0.final gwt 和 ejb 组合
这是我的第一篇文章,所以如果有不明白的地方,请随时提问。
我正在尝试开发一个 gwt 应用程序,它使用来自外部商店项目的我自己的 ejb 类。
我的 ServiceImpl 根据需要到达 ejb,但是我无法使用在我看来的注入。在我的 ejb 类中,我调用一个函数来使用虚拟数据创建我的测试数据库。为此(当然还有后来的任何请求)我想注入一个 EntityManager。这是在我的 Base-Class HomeBase 中完成的。 问题:entityManager 可能未初始化。我尝试了这两个注释:
@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
通过调试该函数,我发现实体管理器为 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();
}
}
发起的类(通过接口产品)ProductHome:
@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;
}
}
this is my first post, so if it isn't understandable, please feel free to ask.
I'm trying to develop a gwt-application that uses my own ejb-classes from an external shop-project.
My ServiceImpl reaches the ejb, as required, however I'm not able to use Injection as it seems to me. In my ejb-class I call a function to create my test-database with dummy-data. For this (and later on for any requests of course) I wanted to inject an EntityManager. This is done in my Base-Class HomeBase.
the problem: The entityManager may not be initialized. I tryed both annotations:
@PersistenceUnit(unitName = "sung.app.kylintv.ejb")
protected EntityManagerFactory entityManagerFactory;
protected EntityManager entityManager = entityManagerFactory.createEntityManager();
as well, as:
@PersistenceContext(unitName="sung.app.kylintv.ejb")
protected EntityManager entityManager;
I'm running jBoss 6.1.0.Final, GWT 2.4 serverside calling my ejb-function.
JBoss starts up correctly, not showing any errors. However, when calling the function I get this error-message:
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
By debugging the function I found the entityManager to be NULL.
How can I get the injection working? Or am I taking a totally wrong approach on this?
For more informations if required:
Code:
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();
}
}
The initiated class (through interface product) ProductHome:
@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;
}
}
The extended 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在尝试从
entityManagerFactory
设置一个EntityManager
,我没有看到它在任何地方初始化。也许您应该检查 protectedEntityManagerFactoryEntityManagerFactory
是否为 null。如果是这样,那就是你的问题了。It looks like you're trying to set an
EntityManager
fromentityManagerFactory
, which I don't see initialized anywhere. Perhaps you should check to see if protectedEntityManagerFactory entityManagerFactory
is null. If so, there's your issue.