本文介绍了OSGi是否使用来自另一个捆绑包的jpa持久性单元?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将ServiceMix与Apache Felix一起使用,并且我的企业应用程序由几个捆绑软件组成.例如,我有一个包含我的jpa实体类的捆绑软件,另一个是具有业务逻辑和dao-classs的捆绑软件.由于我使用的是OpenJPA,因此实体捆绑项目需要在编译时将META-INF文件夹中的persistenc.xml用于字节码注入,但是此捆绑包通过捆绑清单中的Meta-Persistence标头提供了持久单元.在提供dao的捆绑包内部,我使用blueprint.xml注入了实体管理器:

I use ServiceMix with Apache Felix and my Enterprise Application consist of several bundles. For example I have a bundle with my jpa entity classes and another bundle with business logic and dao-classes. Because of I use OpenJPA the entity-bundle project needs the persistenc.xml in META-INF folder for bytecode injection at compiletime, but this bundle provides the persistence unit by the Meta-Persistence header in the bundle manifest.Inside of the dao providing bundle I inject the entity manager by using blueprint.xml:

<bean id="systemUserDAOBean" class="server.daos.SystemUserDAO">
  <tx:transaction method="*" value="Required" />
  <jpa:context property="entityManager" unitname="mypu" />
</bean>

如果我在运行时调用以下dao方法:

if I call at runtime the following method of a dao:

public SystemUser readSystemUser(String username) {
        final EntityManager em = getEntityManager();
        final Query q = em.createQuery("select a from SystemUser a where a.username = '"+username+"'");
        return (SystemUser) q.getSingleResult();
}

将引发ClassCastException:

a ClassCastException will be thrown:

java.lang.ClassCastException:无法将mypackage.SystemUser强制转换为mypackage.SystemUser

到目前为止,我发现的是,加载了entitymanager返回对象的类的类加载器不同于加载了方法的返回类型的类加载器.也许第一个类加载器是实体捆绑包的类加载器,而第二个类加载器则是实体捆绑包的类加载器?!

What I found out until now is, that the class loader which loaded the class of the entitymanager returning object is different to the classloader which loaded the returning type of the method. Maybe the first classloader is the classloader of the entity bundle and the second the classloader of the dao providing bundle?!

如果我将persistence.xml复制到DAO-Bundle中,并在blueprint.xml中使用其持久性单元,则不会引发ClassCastException.但是在这种情况下,我不想在同一个应用程序中包含两个完全相同的persistence.xml文件. :(

If I copy the persistence.xml into the DAO-Bundle and use its persistence unit in the blueprint.xml the ClassCastException will not be thrown. But in this case I have two identically persistence.xml-files inside of the same application, what I dont want. :(

有人知道如何解决该问题吗?

does anyone have an idea how to solve that issue?

谢谢,菲尔

当我重新启动servicemix时,异常消失了,直到我更新了持久性捆绑包.我发现,这两个类加载器都来自持久性捆绑包.

When I restart servicemix the exception is gone until I update the persistence bundle. And I found out, that both classloader come from the persistence bundle.

推荐答案

听起来您已将域类(包括我认为的SystemUser)复制到多个包中.您不应这样做,因为您已经发现,Java将不同的ClassLoader加载的同一类视为不同的类,因此得到了ClassCastException.

It sounds like you have copied the domain classes (including, I assume, SystemUser) into multiple bundles. You should not to this because, as you have discovered, Java considers the same class loaded by different ClassLoaders to be different classes, and hence you get the ClassCastException.

您应该使用Export-Package标头从一个捆绑软件中导出域包.可能应该从您的持久性捆绑包中完成.所有其他捆绑软件都应导入该软件包.

You should export the domain package from one bundle, using the Export-Package header. Probably this should be done from your persistence bundle. All other bundles should import that package.

这篇关于OSGi是否使用来自另一个捆绑包的jpa持久性单元?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 20:13