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:
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.
Write your own Configuration class which extends org.hibernate.cfg.Configuration, In this class you have to
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.)