当前位置: 首页 > 知识库问答 >
问题:

JPA实体管理器未正确注入-Weblogic

万嘉熙
2023-03-14

我有一个EAR应用程序(要部署在WebLogic12c上),它有一个“持久化”组件。“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>
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),尽管我可以在Deployments>“MyDomain”>Configuration/Settings>Persistence下看到MyPersistenceUnitmyDataSource正常且已连接。

有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,但我不知道如何修复它。

如果有人能帮我,我会很感激的。如果需要,可以向我询问更多的代码/配置/上下文。

共有1个答案

刁文光
2023-03-14

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

要获得真正的EJB,您应该在初始上下文中查找bean:

 MyBeanRemote bean = (MyBeanRemote) new InitialContext().lookup("MyBean/remote");
 类似资料:
  • 我刚刚开始使用Spring ROO,并使用数据库逆向工程命令生成了我的实体类。然而每当我试图调用生成的实体类中的一个CRUD方法时,我总是得到这个异常:Java . lang . illegalstateexception:实体管理器没有被注入(Spring Aspects JAR是否被配置为AJC/AJDT方面库?) 我怀疑(通过查看生成的文件)EntityManager没有被注入到类中。你能告

  • 我试图使用spring boot将EntityManager注释注入到我的DAO中,但是得到一个< code > InvalidDataAccessApiUsageException 消息,说没有可用的事务EntityManager。我的印象是,只要Spring Boot从< code>application.yml中获得了我的数据源信息,并且我用< code>@PersitenceContext

  • 目前,我学习JavaEE JPA规范。我使用Hibernate作为JPA实现。我有下一个问题: 我有一个简单的实体 还有一个简单的服务: persistence.xml有下一个视图: 我不明白为什么Hibernate会话没有注入BookService。在作为EntityManager的图书服务中,我得到了org.jboss.as.TransactionScopedEntityManager而不是来

  • 问题内容: 我在数据访问层中将JPA-2.0与Hibernate一起使用。 为了进行审核日志记录,我通过在persistence.xml中配置以下属性来使用Hibernate的EmptyInterceptor: 凡 AuditLogInterceptor 扩展Hibernate的’ org.hibernate.EmptyInterceptor ‘。 我在数据访问层中使用JPA实体管理器来执行数据库

  • 和 只是使用不同的jndi名称(即CHServiceBean com.....)的javax.persistence.EntityManager类型的java:comp/env/chjpa引用,等等)得到了相同的错误。 最后,根据这篇文章:访问EntityManager时的错误-openjpa-自由配置文件似乎是我不能拥有完整的JavaEE7特性并运行JPA2.0吗?请指教!

  • 问题内容: 一个很长的问题,请忍受我。 我们将Spring + JPA用于Web应用程序。我的团队在讨论如何在注入的(基于泛型的东西对AppFuse中提供的线DAO,我们不使用过的注射某种原因)。我们正在使用“应用程序管理的持久性”。 反对注入a的论点是它太重了,因此不是必需的,这就是我们需要的。而且,由于Spring将为每个Web请求创建一个DAO的新实例(我对此表示怀疑),因此不会有任何并发​