问题描述
我有一个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.0encoding =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()
{
返回新的MyBean();
}
}
我使用常规代码: / p>
MyBeanLocal bean = MyBeanFactory.create();
MyBeanLocal是MyBean的界面,它就像这样
@Local(MyBeanLocal.class)
public interface MyBeanLocal {
public void insert(MyObject object);
}
我试图注入 MyBean
实例通过 @EJB
注释,它失败(NullPointerException当我尝试使用 MyBean
实例)
当我的应用程序部署在Weblogic服务器上时,我看不到 MyBean
code> MyPersistenceUnit (我看到其他组件的MDB),虽然我可以看到 MyPersistenceUnit
在部署> MyDomain>配置/设置>持久性。 MyDataSource
可以正常连接。
有weblogic服务器的启动日志。
####< 15 juil。 2015 09 h 52 CEST> <公告> <安全和GT; <名为myUsername> <&的AdminServer GT; < [ACTIVE] ExecuteThread:'0'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946746252> < BEA-090082> <使用安全领域myrealm的安全初始化>
####< 15 juil。 2015 09 h 52 CEST> <公告> < WebLogicServer> <名为myUsername> <&的AdminServer GT; < [STANDBY] ExecuteThread:'3'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946771648> < BEA-000365> <服务器状态改为STANDBY。>
####< 15 juil。 2015 09 h 52 CEST> <公告> < WebLogicServer> <名为myUsername> <&的AdminServer GT; < [STANDBY] ExecuteThread:'3'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946771650> < BEA-000365> <服务器状态更改为STARTING。>
####< 15 juil。 2015 09 h 53 CEST> <警告> < EJB> <名为myUsername> <&的AdminServer GT; < [ACTIVE] ExecuteThread:'3'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946795598> < BEA-012035> < EJB UtilityBean中的远程接口方法:public abstract boolean com.myclasses.UtilityRemote.send(com.mytypes.ReqType,java.lang.String)包含com.mytypes.ReqType类型的参数,该参数不可序列化。虽然EJB IMMJMSUtilityBean已将引用设置为false,但此参数不可序列化,因此将通过引用传递。只有参数类型是可串行化的,才能使用call-by-value传递参数。>
####< 15 juil。 2015 09 h 53 CEST> <公告> <日志管理> <名为myUsername> <&的AdminServer GT; < [ACTIVE] ExecuteThread:'3'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946808540> < BEA-170027> <服务器已成功建立与域级诊断服务的连接。
####< 15 juil。 2015 09 h 53 CEST> <公告> < WebLogicServer> <名为myUsername> <&的AdminServer GT; < [STANDBY] ExecuteThread:'1'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946809347> < BEA-000365> <服务器状态更改为ADMIN。>
####< 15 juil。 2015 09 h 53 CEST> <公告> < WebLogicServer> <名为myUsername> <&的AdminServer GT; < [STANDBY] ExecuteThread:'1'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946809503> < BEA-000365> <服务器状态更改为RESUMING。>
####< 15 juil。 2015 09 h 53 CEST> <公告> <服务器> <名为myUsername> <&的AdminServer GT; < [ACTIVE] ExecuteThread:'3'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946809774> < BEA-002613> < ChannelDefault [1]现在正在侦听127.0.0.1:7001以获取协议iiop,t3,ldap,snmp,http。>
####< 15 juil。 2015 09 h 53 CEST> <警告> <服务器> <名为myUsername> <&的AdminServer GT; < DynamicListenThread [默认]> << WLS内核>> <> <> < 1436946809774> < BEA-002611> <主机名MyUsername.MyProxy.com映射到多个IP地址:172.23.247.5,0:0:0:0:0:0:0:1>
####< 15 juil。 2015 09 h 53 CEST> <公告> <服务器> <名为myUsername> <&的AdminServer GT; < [ACTIVE] ExecuteThread:'3'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946809775> < BEA-002613> < ChannelDefault [2]正在监听0:0:0:0:0:0:0:1:7001,协议iiop,t3,ldap,snmp,http。
####< 15 juil。 2015 09 h 53 CEST> <公告> <服务器> <名为myUsername> <&的AdminServer GT; < [ACTIVE] ExecuteThread:'3'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946809775> < BEA-002613> < ChannelDefault正在侦听协议iiop,t3,ldap,snmp,http。>的172.23.247.5:7001。
####< 15 juil。 2015 09 h 53 CEST> <公告> < WebLogicServer> <名为myUsername> <&的AdminServer GT; < [ACTIVE] ExecuteThread:'3'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946809776> < BEA-000331> <启动在开发模式下运行的域测试的WebLogic Server管理服务器AdminServer。
####< 15 juil。 2015 09 h 53 CEST> <公告> < WebLogicServer> <名为myUsername> <&的AdminServer GT; < [ACTIVE] ExecuteThread:'0'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946810276> < BEA-000360> <服务器以RUNNING模式启动>
####< 15 juil。 2015 09 h 53 CEST> <公告> < WebLogicServer> <名为myUsername> <&的AdminServer GT; < [ACTIVE] ExecuteThread:'0'作为队列:'weblogic.kernel.Default(self-tuning)'> << WLS内核>> <> <> < 1436946810280> < BEA-000365> <服务器状态更改为RUNNING。>
我怀疑问题是这个bean没有被weblogic服务器正确处理,但我有不知道如何解决它。
如果有人可以帮助我,我会感激的。
如果需要,请给我更多的代码/配置/上下文。
重新获取EJB参考 - 如果您使用new自己构建实例,那么您将获得的只是一个POJO而不是EJB - 这意味着您将无法访问任何EJB服务,如依赖注入和事务管理。
要掌握真正的EJB,您应该:
MyBeanRemote bean =(MyBeanRemote )new InitialContext()。lookup(MyBean / remote);
I have an EAR app (to deploy on Weblogic 12c), that has a "persist" componenent. The "persist" component uses JPA (implementation: EclipseLink) to persist the objects.
The bean that uses the entityManager is declared as @Stateless
and the entity manager is injected though the @PersistenceContext
annotation.
The problem is, i have an NullPointerException
each time i try to access the entityManager (meaning, he has not been correctly injected).
@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>
I use a factory to get an instance of MyBean
:
public class MyBeanFactory
{
public static MyBean create()
{
return new MyBean();
}
}
And i use it like this in regular code:
MyBeanLocal bean = MyBeanFactory.create();
MyBeanLocal is the interface for MyBean and it goes like that
@Local(MyBeanLocal.class)
public interface MyBeanLocal {
public void insert(MyObject object);
}
I tried to inject the MyBean
instance through the @EJB
annotation, and it fails (NullPointerException whenever i try to use the MyBean
supposedly injected instance)
When my application is deployed on the Weblogic server, i don't see neither MyBean
, nor MyPersistenceUnit
in it (i see the MDBs from other componenents), though i can see MyPersistenceUnit
under Deployments > "MyDomain" > Configuration/Settings > Persistence. MyDataSource
is okay and connected.
There is the startup log for the weblogic server.
####<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.>
I suspect that the problem is that this bean is not correctly processed by the weblogic server, but i have no idea how to fix it.
If anyone could help me, i'd be thankful. Ask me for more code/config/context if you need to.
The problem here is how you're getting hold of the EJB reference - if you construct the instance yourself using "new", what you'll get is just a POJO rather than an EJB - this means you won't have access to any EJB services like dependency injection and transaction management.
To get hold of a genuine EJB, you should look up your bean in the initial context:
MyBeanRemote bean = (MyBeanRemote) new InitialContext().lookup("MyBean/remote");
这篇关于JPA实体经理没有正确注入 - Weblogic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!