在WebSphere Liberty中注入JPA实体管理器时出错

时间:2016-04-05 19:17:39

标签: jpa java-ee websphere-liberty

我继承了最初使用WebSphere 6.1构建的遗留应用程序,然后迁移到运行JPA 2.0和openJPA的WebSphere 8.0,没有任何问题。出于战略原因,我们正在迁移到WebSphere Liberty。我们首先在WebSphere Classic 8.5.5.8和JPA上进行了测试,实体管理器在那里没有任何问题。但是,在Liberty 8.5.5.8上,我得到以下异常:

  

javax.ejb.EJBException:对于CHServiceEAR应用程序的CHServiceEJB.jar模块中的CHServiceBean组件,类型为javax.persistence.EntityManager的java:comp / env / com.xxx.xxxx.service.CHServiceBean / em引用不能得到解决。       at com.ibm.wsspi.injectionengine.InjectionBinding.getInjectionObject(InjectionBinding.java:1493)   .....   [错误]引起:   [错误] javax.ejb.EJBException:CHServiceEAR应用程序的CHServiceEJB.jar模块中的CHServiceBean组件的类型为javax.persistence.EntityManager的java:comp / env / com.xxxx.xxxx.service.CHServiceBean / em引用不能得到解决。   [err] at com.ibm.wsspi.injectionengine.InjectionBinding.getInjectionObject(InjectionBinding.java:1493)   [内部班级] [错误]

我有另一个EJB注入问题,通过配置绑定文件解决了,但是我无法解决这个问题。我有两个应用程序,每个应用程序都有自己的EAR文件,但都在同一个Liberty JVM中运行。应用程序A运行前端/ UI逻辑,而应用程序B是后端EJB / JPA接口。在项目方面,JPA应用程序设置为2.0(我想要2.1,但基于另一个线程,JPA 2.0和EJB 3.1目前可以达到最高...请参阅我的其他主题 - > {{ 3}})。

这是我的server.xml文件:

    <server description="new server">

    <!-- Enable features -->
    <featureManager>
        <feature>javaee-7.0</feature>
        <feature>localConnector-1.0</feature>
        <feature>distributedMap-1.0</feature>
        <feature>adminCenter-1.0</feature> 
        <feature>ssl-1.0</feature>
        <feature>usr:webCacheMonitor-1.0</feature>
        <feature>webCache-1.0</feature>
        <feature>ldapRegistry-3.0</feature>
    </featureManager>

    <!--  Admin Center Config Start -->
    <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
    <httpEndpoint host="*" httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>

    <keyStore id="defaultKeyStore" password="xxxxxx"/> 

    <basicRegistry id="basic"> 
          <user name="admin" password="xxxxx"/> 
          <user name="nonadmin" password="xxxxxx"/> 
    </basicRegistry> 

    <administrator-role> 
      <user>admin</user>
    </administrator-role>

    <remoteFileAccess> 
       <writeDir>${server.config.dir}</writeDir> 
    </remoteFileAccess>

    <!-- Automatically expand WAR files and EAR files -->
    <applicationManager autoExpand="true"/>
    <applicationMonitor updateTrigger="mbean"/>

    <enterpriseApplication id="CHNewCHRDMEAR" location="CHNewCHRDMEAR.ear" name="CHNewCHRDMEAR">
        <application-bnd>
            <security-role name="AllAuthenticated">
                <special-subject type="ALL_AUTHENTICATED_USERS"/>
            </security-role>
        </application-bnd>
    </enterpriseApplication>
    <enterpriseApplication id="CHServiceEAR" location="CHServiceEAR.ear" name="CHServiceEAR"/>

    <!--  JAAS Authentication Alias (Global) Config -->
    <authData id="dbUser" password="{xor}MzhmJT06ajI=" user="dbUser"/>

    <!--  JDBC Driver and Datasource Config -->
    <library id="DB2JCC4Lib">
        <fileset dir="C:\DB2\Jars" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>
    </library>

    <dataSource containerAuthDataRef="dbUser" id="CHTEST2" jndiName="jdbc/nextgen" type="javax.sql.XADataSource">
        <jdbcDriver libraryRef="DB2JCC4Lib"/>
        <properties.db2.jcc databaseName="CHTEST2" password="{xor}MzhmJT06ajI=" portNumber="60112" serverName="server.com" sslConnection="false" user="dbUser"/>
        <containerAuthData password="{xor}MzhmJT06ajI=" user="dbUser"/>
    </dataSource>

    <dataSource id="CHTEST2_RO" jndiName="jdbc/nextgen_RO" type="javax.sql.XADataSource">
        <jdbcDriver libraryRef="DB2JCC4Lib"/>
        <properties.db2.jcc databaseName="CHTEST2" password="{xor}MzhmJT06ajI=" portNumber="60112" serverName="server.com" sslConnection="false" user="dbUser"/>
        <containerAuthData password="{xor}MzhmJT06ajI=" user="dbUser"/>
    </dataSource>
<!-- More in file, but no included...-->
</server>

这是我的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"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
          http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">
    <persistence-unit name="CHService" transaction-type="JTA">
    <jta-data-source>jdbc/nextgen</jta-data-source>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="openjpa.jdbc.TransactionIsolation" value="read-uncommitted" /></properties></persistence-unit>
    <persistence-unit name="CHServiceRO" transaction-type="JTA">
        <jta-data-source>jdbc/nextgen_RO</jta-data-source>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="openjpa.jdbc.TransactionIsolation" value="read-uncommitted" />
</properties>
</persistence-unit>
</persistence>

我相信我们完全依赖注入来获取JPA jndi查找的上下文,但这是因为我在代码中没有看到对任何JPA特定JNDI名称的初始上下文的任何调用。以下是来自EJB项目的两个anotated会话bean:

一个。 CHService Bean:

    @Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@Local({ CHServiceLocal.class })
@Remote({ CHServiceRemote.class })
@Interceptors({ CHServiceLog.class })
@Resources({
        @Resource(name = "jdbc/nextgen", mappedName = "jdbc/nextgen", authenticationType = AuthenticationType.APPLICATION, shareable = true, type = javax.sql.DataSource.class),
        @Resource(name = "services/cache/CHBluepages", mappedName = "services/cache/CHBluepages", authenticationType = AuthenticationType.APPLICATION, shareable = true, type = com.ibm.websphere.cache.DistributedMap.class),
        @Resource(name = "services/cache/CHGeneric", mappedName = "services/cache/CHGeneric", authenticationType = AuthenticationType.APPLICATION, shareable = true, type = com.ibm.websphere.cache.DistributedMap.class) })
public class CHServiceBean extends AbstractCHServiceImpl implements
        CHService {

    @PersistenceContext(unitName = "CHService")
    private EntityManager em;

湾CHServiceRO bean:

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@Local({CHServiceLocalRO.class})
@Remote({CHServiceRemoteRO.class})
@Interceptors({CHServiceROLog.class})
@Resources({
    @Resource(name="jdbc/nextgen_RO", mappedName="jdbc/nextgen_RO", authenticationType=AuthenticationType.APPLICATION, shareable=true, type=javax.sql.DataSource.class),
    @Resource(name="jdbc/nextgen", mappedName="jdbc/nextgen", authenticationType=AuthenticationType.APPLICATION, shareable=true, type=javax.sql.DataSource.class),
    @Resource(name="services/cache/CHBluepages", mappedName="services/cache/CHBluepages", authenticationType=AuthenticationType.APPLICATION, shareable=true, type=com.ibm.websphere.cache.DistributedMap.class),
    @Resource(name="services/cache/CHGeneric", mappedName="services/cache/CHGeneric", authenticationType=AuthenticationType.APPLICATION, shareable=true, type=com.ibm.websphere.cache.DistributedMap.class)
})
public class CHServiceBeanRO implements CHServiceRO {

    @PersistenceContext (unitName="CHServiceRO") private EntityManager em;

    private CHServiceBase ch;

    @PostConstruct
    private void init() { ch = new CHServiceBase(em); }

以下是调用JPA应用程序的前端应用程序的Web.xml的片段:

<resource-ref id="ResourceRef_1436377001246">
        <res-ref-name>jdbc/nextgen</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Application</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <resource-ref id="ResourceRef_1436377001247">
        <res-ref-name>jdbc/nextgen_RO</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Application</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>

根据Gas关于此主题的帖子:Eclipse Juno and JPA 2.1 support

我还尝试使用以下条目更新web.xml:

<persistence-unit-ref>
  <persistence-unit-ref-name>chJPA</persistence-unit-ref-name>
  <persistence-unit-name>CHService</persistence-unit-name>
</persistence-unit-ref>
<persistence-unit-ref>
  <persistence-unit-ref-name>chJPA_RO</persistence-unit-ref-name>
  <persistence-unit-name>CHServiceRO</persistence-unit-name>
</persistence-unit-ref>

和Bean代码:

@PersistenceContext(name = "chJPA", unitName = "CHService")

@PersistenceContext (name="chJPA_RO", unitName="CHServiceRO")

只是使用不同的jndi名称得到了相同的错误,即javax.persistence.EntityManager类型的java:comp / env / chJPA引用,用于CHServiceBean com .......等等。

最后,根据这篇文章:java.lang.ClassCastException,Getting Entitymanager Via JNDI Lookup 似乎我可以拥有完整的JavaEE 7功能并运行JPA 2.0?请指教!

2 个答案:

答案 0 :(得分:2)

正如您所指的帖子一样 - 您不能将<feature>javaee-7.0</feature>与JPA 2.0一起使用,因为它启用2.1,这就是您遇到冲突的原因。

所以你有两个选择:

  • 使用Java EE7和JPA 2.1
  • 或只启用所需的Java EE 6功能,然后使用JPA 2.0

由于您要从现在不支持Java EE7的WAS 8.0迁移,因此更容易的选择可能是使用第二个选项。 因此,请尝试删除javee-7.0功能,并添加ejbLite-3.1jpa-2.0以及您需要的更多内容。

答案 1 :(得分:0)

你没有javaee-7.0和JPA 2.0功能是正确的,因为它启用了JPA 2.1功能。所以Gas给出的答案是正确的。

我只是想指出,因为你说你最初想要转到JPA 2.1,一旦你解决了你的eclipse问题,你应该使用WebSphere Application Server Migration Toolkit来识别从JPA迁移时所需的应用程序更改2.0至JPA 2.1。 Liberty JPA 2.0实现基于OpenJPA构建,而JPA 2.1实现基于EclipseLink构建。迁移工具包可以在wasdev上免费下载:https://developer.ibm.com/wasdev/downloads/#asset/tools-WebSphere_Application_Server_Migration_Toolkit