无法在JBoss AS 7.1.1中注入EntityManager

时间:2013-10-13 18:37:21

标签: java jpa struts2 jboss nullpointerexception

在JBoss AS 7.1.1上部署我的应用程序时,我遇到了EntityManager注入问题。应用程序使用Struts2和Struts2 CDI插件。除了EntityManager注入CDI工作正常 - 只注入EntityManager。这个错误是JBoss特有的 - 同样的应用程序在GlassFish 3.1.2.2 Web Profile下没有错误。应用程序打包为war,其中分离的core-0.2.jar(应用程序逻辑)位于WEB-INF / lib文件夹中。我不使用Spring等。

堆栈追踪:

20:07:52,219 ERROR [org.apache.struts2.dispatcher.Dispatcher] (http--127.0.0.1-8090-1) Exception occurred during processing request: null: java.lang.NullPointerException
    at proj.SystemUserRepositoryImpl.fetchByLogin(SystemUserRepositoryImpl.java:23) [core-0.2.jar:]
    at proj.SystemUserServiceImpl.getCurrentUserInfo(SystemUserServiceImpl.java:31) [core-0.2.jar:]
    at proj.web.GetAction.execute(GetAction.java:60) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_40]
    at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_40]
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) [struts2-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) [struts2-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73) [struts2-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91) [struts2-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252) [struts2-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) [struts2-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) [struts2-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:546) [struts2-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) [struts2-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) [struts2-core-2.3.14.3.jar:2.3.14.3]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_40]

项目结构:

core -> core-0.2.jar
    src
        main
            java
                proj
                    SystemUserServiceImpl.java
                    SystemUserRepositoryImpl.java
                    ...
            resources
                META-INF
                    beans.xml
                    persistence.xml
                    ...
web -> web.war
    src
        main
            java
                proj.web
                    GetAction.java
                    ...
            resources
                META-INF
                    beans.xml
            webapp
                WEB-INF
                    web.xml
                    ...
                ...
            ...

GetAction.java:

@Action("get/{id}")
public class GetAction extends ActionSupport {

    @Inject
    private SystemUserService systemUserService;

    public SystemUserService getSystemUserService() {
        return systemUserService;
    }

    public void setSystemUserService(SystemUserService systemUserService) {
        this.systemUserService = systemUserService;
    }

    @Override
    public String execute() throws Exception {
        CurrentSystemUserInfo currentUserInfo = systemUserService.getCurrentUserInfo();

    ...

SystemUserServiceImpl.java:

@Named
public class SystemUserServiceImpl implements SystemUserService {

    @Inject
    private SystemUserRepository systemUserRepository;

    public SystemUserRepository getSystemUserRepository() {
        return systemUserRepository;
    }

    public void setSystemUserRepository(SystemUserRepository systemUserRepository) {
        this.systemUserRepository = systemUserRepository;
    }

    @Override
    @TransactionAttribute(value = TransactionAttributeType.REQUIRED)
    public CurrentSystemUserInfo getCurrentUserInfo() {
        SystemUser currentUserData = systemUserRepository.fetchByLogin(SystemUser.DEFAULT_LOGIN);

...

SystemUserServiceImpl.java:

public class SystemUserRepositoryImpl implements SystemUserRepository {

private EntityManager entityManager;

public EntityManager getEntityManager() {
    return entityManager;
}

@PersistenceContext(unitName="MyApp")
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

@Override
public SystemUser fetchByLogin(String login) {
    Query query = entityManager.createQuery("SELECT u FROM SystemUser u WHERE login = :login");

...

的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="MyApp" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/jdbc/myapp</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.dialect" value="proj.DerbyDialect"/>
      <property name="hibernate.ejb.naming_strategy" value="proj.DatabaseStructureNamingStrategy"/>
      <property name="hibernate.show_sql" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

JBoss 7.1.1配置:

    <subsystem xmlns="urn:jboss:domain:datasources:1.0">
        <datasources>
            <datasource jndi-name="java:jboss/jdbc/myapp" pool-name="MyApp">
                <connection-url>jdbc:derby://localhost/myappdb;create=true</connection-url>
                <driver>derby</driver>
                <security>
                    <user-name>myappu</user-name>
                    <password>myappp</password>
                </security>
            </datasource>
            <drivers>
                <driver name="derby" module="org.apache.derby">
                    <driver-class>org.apache.derby.jdbc.ClientDriver</driver-class>
                    <xa-datasource-class>org.apache.derby.jdbc.ClientXADataSource</xa-datasource-class>
                </driver>
            </drivers>
        </datasources>
    </subsystem>

我做错了什么?任何帮助都会得到满足。

1 个答案:

答案 0 :(得分:0)

尝试将Hibernate用作JBoss AS 7的模块。

创建/modules/org/hibernate/3/module.xml

module.xml

<module xmlns="urn:jboss:module:1.0" name="org.hibernate" slot="3">
    <resources>
        <resource-root path="hibernate-core-3.6.1.Final.jar"></resource-root>
        <resource-root path="hibernate-entitymanager-3.6.1.Final.jar"></resource-root>
        <resource-root path="hibernate-commons-annotations-3.2.0.Final.jar"></resource-root>
        <resource-root path="asm-3.3.jar"></resource-root>
        <resource-root path="antlr-3.2.jar"></resource-root>
        <resource-root path="btm-1.3.3.jar"></resource-root>
    </resources>
    <dependencies>
        <module name="org.jboss.as.jpa.hibernate" slot="3"></module>
        <module name="javax.api"></module>
        <module name="javax.persistence.api"></module>
        <module name="javax.transaction.api"></module>
        <module name="javax.validation.api"></module>
        <module name="org.antlr"></module>
        <module name="org.apache.ant"></module>
        <module name="org.apache.commons.collections"></module>
        <module name="org.dom4j"></module>
        <module name="org.infinispan"></module>
        <module name="org.javassist"></module>
        <module name="org.jboss.logging"></module>
        <module name="org.slf4j"></module>
    </dependencies>
</module>

有关详情,请参阅here