【问题标题】:JPA entity manager not correctly injected - WeblogicJPA 实体管理器未正确注入 - Weblogic
【发布时间】:2015-09-29 08:12:10
【问题描述】:

我有一个 EAR 应用程序(部署在 Weblogic 12c 上),它有一个“持久”组件。 “persist”组件使用 JPA(实现:EclipseLink)来持久化对象。

使用 entityManager 的 bean 被声明为 @Stateless,并且通过 @PersistenceContext 注解注入了实体管理器。

问题是,我每次尝试访问 entityManager 时都有一个NullPointerException(意思是,他没有被正确注入)。

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class MyBean implements MyBeanLocal {    

    @PersistenceContext(unitName = "MyPersistenceUnit", type = PersistenceContextType.EXTENDED)
    EntityManager entityManager;

    public void insert(MyObject object) {
        try {
            entityManager.persist(object); //NullPointerException here
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

persistence.xml

<?xml version="1.0"  encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
              http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="MyPersistenceUnit"
        transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>MyDataSource</jta-data-source>
        <class>com.myclasses.MyObject</class>>
    </persistence-unit>

</persistence>

我使用工厂获取MyBean的实例:

public class MyBeanFactory
{
    public static MyBean create()
    {
        return new MyBean();
    }
}

我在常规代码中这样使用它:

MyBeanLocal bean = MyBeanFactory.create();

MyBeanLocal 是 MyBean 的接口,它是这样的

@Local(MyBeanLocal.class)
public interface MyBeanLocal {
    public void insert(MyObject object);
}

我尝试通过 @EJB 注释注入 MyBean 实例,但它失败了(每当我尝试使用据称注入的 MyBean 实例时出现 NullPointerException)

当我的应用程序部署在 Weblogic 服务器上时,我既看不到 MyBean,也看不到 MyPersistenceUnit(我看到来自其他组件的 MDB),但我可以看到 下的 MyPersistenceUnit部署 > “我的域” > 配置/设置 > 持久性MyDataSource 正常且已连接。

有weblogic服务器的启动日志。

####<15 juil. 2015 09 h 52 CEST> <Notice> <Security> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946746252> <BEA-090082> <Security initializing using security realm myrealm.> 
####<15 juil. 2015 09 h 52 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946771648> <BEA-000365> <Server state changed to STANDBY.> 
####<15 juil. 2015 09 h 52 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946771650> <BEA-000365> <Server state changed to STARTING.> 
####<15 juil. 2015 09 h 53 CEST> <Warning> <EJB> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946795598> <BEA-012035> <The Remote interface method: public abstract boolean com.myclasses.UtilityRemote.send(com.mytypes.ReqType,java.lang.String) in EJB UtilityBean contains a parameter of type com.mytypes.ReqType which is not serializable. Though the EJB IMMJMSUtilityBean has call-by-reference set to false, this parameter is not serializable and hence will be passed by reference. A parameter can be passed using call-by-value only if the parameter type is serializable.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <Log Management> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946808540> <BEA-170027> <The server has successfully established a connection with the Domain level Diagnostic Service.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809347> <BEA-000365> <Server state changed to ADMIN.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809503> <BEA-000365> <Server state changed to RESUMING.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <Server> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809774> <BEA-002613> <Channel "Default[1]" is now listening on 127.0.0.1:7001 for protocols iiop, t3, ldap, snmp, http.> 
####<15 juil. 2015 09 h 53 CEST> <Warning> <Server> <MyUsername> <AdminServer> <DynamicListenThread[Default]> <<WLS Kernel>> <> <> <1436946809774> <BEA-002611> <The hostname "MyUsername.MyProxy.com", maps to multiple IP addresses: 172.23.247.5, 0:0:0:0:0:0:0:1.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <Server> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809775> <BEA-002613> <Channel "Default[2]" is now listening on 0:0:0:0:0:0:0:1:7001 for protocols iiop, t3, ldap, snmp, http.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <Server> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809775> <BEA-002613> <Channel "Default" is now listening on 172.23.247.5:7001 for protocols iiop, t3, ldap, snmp, http.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809776> <BEA-000331> <Started the WebLogic Server Administration Server "AdminServer" for domain "test" running in development mode.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946810276> <BEA-000360> <The server started in RUNNING mode.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946810280> <BEA-000365> <Server state changed to RUNNING.> 

我怀疑问题是weblogic服务器没有正确处理这个bean,但我不知道如何修复它。

如果有人可以帮助我,我将不胜感激。 如果需要,请向我询问更多代码/配置/上下文。

【问题讨论】:

  • 你能贴出你用来获取 MyBean 实例的代码吗?如果我们可以将启动日志放入其中,启动日志也可能会有所帮助,因为听起来 weblogic 无法部署 bean。
  • 我用您要求的信息编辑了原始消息,启动日志中似乎没有任何问题

标签: java jpa ejb weblogic ear


【解决方案1】:

这里的问题是您如何获取 EJB 引用 - 如果您自己使用“new”构建实例,您将获得的只是 POJO 而不是 EJB - 这意味着您将没有访问任何 EJB 服务,例如依赖注入和事务管理。

要获得真正的 EJB,您应该look up your bean in the initial context

 MyBeanRemote bean = (MyBeanRemote) new InitialContext().lookup("MyBean/remote");

【讨论】:

  • 两者都可以(我刚刚使用了 Remote,因为那是链接中的内容,我很懒惰)。只需检查您是否使用了正确的 JNDI 查找并转换为您想要的类型。
  • 抱歉,我通过误点击删除了我的评论(“它是在本地工作还是必须是远程的”)。问题是 MyBean 似乎没有部署在 weblogic 服务器上,因此,我既不能在 JNDI 树中看到它,也不能使用你给我的代码查找它。问题是在 EAR 的另一个组件(“EJB”组件)中声明的无状态 bean 已正确部署,但不是“PERSIST”组件中的那些,即使在 Eclipse 上工作时,我可以看到它确实识别无状态 bean (screenshot)。
  • 对不起,双重评论,实际上我通过将“PERSIST”组件添加为 EJB 模块并使用“java:global.EARNAME.COMPONENETNAME.MyBean!com.myclasses.MyBeanLocal”解决了这个问题但我仍然有一个EJB Exception 作为Extended Persistence Contexts can only be invoked from within the context of the stateful session bean that declares the Extended Persistence Context." at weblogic.persistence.ExtendedEntityManagerProxyImpl.getPersistenceContext(ExtendedEntityManagerProxyImpl.java:62)
  • 太棒了!你想把它作为一个单独的问题提出吗?我对扩展持久性上下文相当生疏,但这听起来很有趣!
  • 该异常的问题是我在使用无状态 EJB 时使用了扩展的持久性上下文。我改为使用事务范围的持久性上下文,它工作得很好。不需要再问了。谢谢你的帮助休!
猜你喜欢
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 2012-05-21
  • 2013-10-13
  • 2011-03-23
  • 1970-01-01
  • 2011-03-15
  • 2020-04-22
相关资源
最近更新 更多