Search code examples
hibernateosgijbossfuse

How can I dynamically add property from .cfg (OSGI) to persistence.xml (Hibernate)


The following case:

in my test_property.cfg file i have the property line:

...
hibernate/persistence.schema=TEST_1
...

in the persistence.xml i have the setting line:

...
<property name="hibernate.default_schema" value="${hibernate/persistence.schema}" />
....

But after build the project the value ${hibernate/persistence.schema} - did not replaced


xml files where I use blueprint tag - is ok, but I try to find out how to use replaceable values for persistence.xml from test property.cfg


Solution

  • Cfg files are used only at runtime and not at build phase.

    -At build phase you can resolve maven variables as you like by enabling ressources feltering on the folder containing your persistance.xml (here is some useful documentation).

    -At runtime, it is not possible to bind a property from cfg file to an xml other file. Don't forget that the bundle is already built so it is a jar which is immutable.

    My solution:

    -define a new bean that consume the property from cfg. Here we are sure that any changes in the cfg at runtime will be immediately detected.

    -in that bean, read the xml file and override the wanted property from the one already read from cfg.