jboss 6.1.0.final gwt 和 ejb 组合

发布于 2024-12-06 03:47:36 字数 3474 浏览 0 评论 0原文

这是我的第一篇文章,所以如果有不明白的地方,请随时提问。

我正在尝试开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

血之狂魔 2024-12-13 03:47:36

看起来您正在尝试从 entityManagerFactory 设置一个 EntityManager ,我没有看到它在任何地方初始化。也许您应该检查 protected EntityManagerFactoryEntityManagerFactory 是否为 null。如果是这样,那就是你的问题了。

It looks like you're trying to set an EntityManager from entityManagerFactory, which I don't see initialized anywhere. Perhaps you should check to see if protected EntityManagerFactory entityManagerFactory is null. If so, there's your issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文