如果DAO不是无状态的,Glassfish不会启动EntityManager [英] Glassfish doesn't bring up EntityManager if DAO is not Stateless

查看:128
本文介绍了如果DAO不是无状态的,Glassfish不会启动EntityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有EJB模块的EAR应用程序,它包含一个持久性单元和许多EJB(作为服务和DAO层)。

  @Stateless 
public class BranchDAO {
@PersistenceContext
private EntityManager entityManager;
}

不建议使用DAO作为无状态bean。所以我使用CDI创建了这个注释:

  @Dependent 
@Stereotype
@Target(ElementType.TYPE )
@Retention(RetentionPolicy.RUNTIME)
public @interface DAO {
}

将我的DAO更改为不使用 @Stateless

  @DAO 
public class BranchDAO {
@PersistenceContext
private EntityManager entityManager;
}

但Glassfish在应用程序启动时不会显示实体管理器。当我调用DAO时,实体管理器处于非法状态。


java.lang.IllegalStateException:无法检索UnitName的EntityManagerFactory null


这个错误只出现在Glassfish 3中,但不在JBoss AS 6中。使用JBoss AS 6我可以看到Hibernate日志初创公司(但我没有看到他们与Glassfish)。

作为一个临时解决方案,我创建了一个无状态bean,其内容如下。它不是漂亮的解决方案,但在Glassfish中运行良好。

  @Stateless 
@Startup
public class AutoStartEntityManager {

@PersistenceContext
私人EntityManager entityManager;






所以,我可以如何强制Glassfish启动EntityManager我在DAO中没有使用 @Stateless

试试显式指定unitName:

$ pre code $ @PersistenceContext(unitName =yourJPAUnitName)
私有EntityManager管理器;

(一个旁注 - 你确定你需要依赖范围的DAO吗?不应该是单例?)


I have an EAR application with an EJB module, that contains one persistence unit and many EJBs (as service and DAO layer).

@Stateless
public class BranchDAO {
    @PersistenceContext
    private EntityManager entityManager;
}

But DAOs as Stateless beans are not recommended. So I create this annotation using CDI:

@Dependent
@Stereotype
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DAO {
}

After my DAO is changed to not use @Stateless:

@DAO
public class BranchDAO {
    @PersistenceContext
    private EntityManager entityManager;
}

But the Glassfish doesn't bring up the entity manager when the application starts. And when I call the DAO, the entity manager is in an illegal state.

java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null

This error only occurs in Glassfish 3, but not in JBoss AS 6. Using JBoss AS 6 I can see the Hibernate logs in startup (but I don't see them with Glassfish).

As a temporary solution I created an Stateless bean with the content below. It's not beautiful solution, but works fine in Glassfish.

@Stateless
@Startup
public class AutoStartEntityManager {

    @PersistenceContext
    private EntityManager entityManager;

}

So, how I can force Glassfish to bring up EntityManager when I'm not using @Stateless in my DAO?

解决方案

Try specifying explicitly the unitName:

@PersistenceContext(unitName="yourJPAUnitName")
private EntityManager manager;

(A sidenote - are you sure you need the DAO in dependent scope? Shouldn't it be singleton?)

这篇关于如果DAO不是无状态的,Glassfish不会启动EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆