OSGi Fragment捆绑Hibernate具有运行时间pojos [英] OSGi Fragment bundle with Hibernate having Runtime pojos

查看:123
本文介绍了OSGi Fragment捆绑Hibernate具有运行时间pojos的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是使用休眠映射各种数据库(特别是SQL Server,MySQl和Postgres);从db记录创建一个xml文件。



对于hibernate,我使用JAssist在运行时创建hbm文件和pojos。
我的代码工作得很好,为了进一步模块化,我为每个数据库实现了片段捆绑包,因此我的主机捆绑包将处理运行时类创建并将它们添加到类加载器,hbm文件创建逻辑和BL中。片段通过传递参数来调用它。

当我为每个数据库创建一个片段包时,
在我的宿主包中创建的运行时pojo类在我的片段包中可见,
i checked与Thread.currentThread()。getContextClassLoader()。loadClass()
并且能够创建它的实例,



问题是
我从片段包中调用Hibernate函数,我得到实体未映射,AFAIK当hibernate无法找到带有表的映射类时,会发生这些异常。
所以我猜Hibernate没有找到我的运行时pojo类。它可以在主机中找到它。 b
$ b

主机:
运行时Pojo创建,
HBM和CFG创建和更新逻辑
BL



片段:
Hibernate层,
调用Hibernate函数,
XML创建逻辑

解决方案 div>

如果你在多个bundle上使用Hibernate,这个问题总会出现。在Hibernate配置中,无法知道哪个Bundle中可以找到映射文件和pojo类文件。 Hibernate不使用OSGI为此提供的机制。因此,hibernate只能找到与Hibernate库在同一个bundle中的映射文件和类。



我不知道是否有任何专业解决方案(a第三方产品)解决此问题。



有两种可能性来解决这个问题:

$ ol

  • 忘记你的分片包,并将所有Hibernate库,映射文件,pojos,使用所有数据库的Hibernate / HQL的类放入一个包中。当您使用不同的hibernate.cfg.xml文件时,您可以在不同的数据库之间切换;每个数据库都有自己的配置文件。这些hibernate.cfg.xml文件可以在bundle之外。

  • 编写你自己的Configuration类,它扩展了org.hibernate.cfg.Configuration,在这个类中你不得不


    • 编写自己的类加载器,它甚至可以在其他包中找到pojo类

    • addResource(String resourceName,ClassLoader classLoader)以一种方式在其他包中找到资源
    • 覆盖doConfigure和buildSessionFactory,因此它们使用类加载器而不是标准类加载器(使用Thread.setContextClassLoader并从超类中调用该方法,即从标准Hibernate配置类中调用该方法)。
    • 覆盖返回配置实例的所有其他方法,以便它们返回配置类的实例并而不是Hibernate Configuration类。



    • 我们做了解决方案2.它是一个有点工作,但现在运行良好。 (想想,当再次改变Hibernate版本时可能需要一些工作。)


      My requirement is to map various database (particularly SQL Server, MySQl and Postgres) using hibernate ; from db record create a xml file.

      For hibernate i am creating hbm files and pojos at run-time using JAssist. My code works great, for further modularization i implement fragment bundles for each database.so that my host bundle will handle runtime class creation and adding them in classloader, hbm file creation logic, and BL. fragment calls it by passing parameters.

      when i create a fragment bundle for each database, the runtime pojo class created in my host bundle is visible in my fragment bundle, i checked with "Thread.currentThread().getContextClassLoader().loadClass()" and able to create its instance,

      The Problem is when i call Hibernate functions from fragment bundle, I am getting "Entity not mapped", AFAIK These exception comes when hibernate is unable to find the mapping class with table. So i guess Hibernate is not finding my runtime pojo classes. which it can find in host.

      Host : Runtime Pojo creation, HBM and CFG creation and updation logic BL

      Fragment : Hibernate layer, Calling Hibernate function, XML Creation logic

      解决方案

      This problem always appears if you use Hibernate over more than one bundle. In the Hibernate configuration you can't tell in which Bundle the mapping files and the pojo class files can be found. Hibernate does not use the mechanisms which OSGI provides for this. As a result hibernate only finds mapping files and classes which are in the same bundle as the Hibernate library.

      I don't know if there is anywhere a professional solution (a third party product) for this problem.

      There are two possibilities to solve this problem:

      1. Forget your fragment bundles and put all Hibernate libraries, mapping files, pojos, classes using Hibernate/HQL for all databases into a single bundle. You can switch between different databases when you use different hibernate.cfg.xml files; each database has its own configuration file. These hibernate.cfg.xml files can be outside the bundles.

      2. Write your own Configuration class which extends org.hibernate.cfg.Configuration, In this class you have to

        • write your own class loader which finds the pojo classes even in other bundles
        • override addResource(String resourceName, ClassLoader classLoader) in a way it finds resources also in other bundles
        • override doConfigure and buildSessionFactory so they use your class loader instead of the standard class loader (use Thread.setContextClassLoader and call the method from the super class, i. e. from the standard Hibernate Configuration class).
        • override all the other methods which return an instance of Configuration so they return the instance of your Configuration class and not of the Hibernate Configuration class.

      We did solution 2. It was a bit of work, but now it is running well. (Thought, when changing the Hibernate version again a bit of work might be necessary.)

      这篇关于OSGi Fragment捆绑Hibernate具有运行时间pojos的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆