EJB+JBOOs运行test程序,EntityManager is null
2015-01-18 14:04
218 查看
EJB @PersistenceContext EntityManager Throws NullPointerException
1.junit 测试时,无法使用依赖注入的方式,得到EntityManger@PersistenceContext(unitName = "trust")
protected EntityManager em=factory.createEntityManager();
目前JBoss 4.2集成了的Tomcat版本为5.5,但Tomcat 6.0以后的版本中才支持使用注释,所以如果将本例中Servlet运行在JBoss 4.2中,并不能获得EntityManagerFactory对象;但在符合J2EE 5.0的服务器中,这样运行是可以的。
虽然在目前JBoss 4.2版本中不支持使用注释,但可以通过另一种方式来获得应用托管的EntityManager对象
解决方案:
1.-改为通过工厂来新建
private static EntityManagerFactory factory;
static{
factory=Persistence.createEntityManagerFactory("jpaUnit");
}
protected EntityManager em=factory.createEntityManager();
外文链接:http://stackoverflow.com/questions/20592925/ejb-persistencecontext-entitymanager-throws-nullpointerexception
http://stackoverflow.com/questions/6469751/testing-an-ejb-with-junit
相关文章推荐
- Could not open JPA EntityManager for transaction; nested exception is java.lang.NullPointerException
- Spring Data Jpa 自定义 Repository EntityManager is null
- 复制功能,比较经典的一段程序,IsDBNull()、User.Identity.Name、myReader.GetString()、取得下拉选框的值:DropDownList1.SelectedValue
- Specified field type [interface javax.persistence.EntityManager] is incompatible with resource type [javax.persistence.EntityMan
- 运行Android程序出错:The connection to adb is down, and a severe error has occured
- C/C++中利用空指针(NULL),提高程序运行效率
- Spring 3.1 entityManagerFactory java.lang.NoSuchFieldError: NULL Error
- C/C++中利用空指针(NULL),提高程序运行效率
- 解决 Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects
- myeclipse下配置运行EJB程序的环境
- java程序运行时出现问题:Could not find the main class: test_jena. Program will exit.
- 程序运行出现错误:System.Web.UI.ClientScriptManager.EnsureEventValidationFieldLoaded
- 通用权限管理系统组件 (GPM - General Permissions Manager) 不需要任何配置文件,程序都可以正常运行
- eclispe无法运行java web程序—提示:Project facet Java version 1.7 is not supported
- linux 使用非当前登录运行程序方式,如root登录用test运行一个test.sh
- 无法安装或运行应用程序。该应用程序要求首先在“全局程序集缓存(gac)”中安装程序集system.data.entity
- 使用flot绘图出现window.G_vmlCanvasManager is null or not an Object
- eclispe无法运行java web程序—提示:Project facet Java version 1.7 is not supported
- 出现错误:The method getCriteriaBuilder() is undefined for the type EntityManager.