One Model mapped on several database type (JPA)


stephane
 

Hello,

Is it possible to have one mapping (file I guess) per database type, which are chosen at runtime "automatically" ?
ex:
orm-mysql.xml
orm-h2.xml
...

My need is I'm running the same application on different database. One is H2 in-memory for unit tests. As some database types used for the target database (ex: Mysql) are not supported by H2, I need to tweak a little bit...

thanks.

--
Stephane


Andy
 

The ORM file is chosen based on the persistence property javax.jdo.option.Mapping, as per the JDO spec.


stephane
 

I'm trying but I can't make it works. I'm using JPA.

I added a package-h2.orm at the root of the project.

With the option javax.jdo.option.Mapping, DN log a warning property not supported
With the option datanucleus.Mapping, DN still load the default orm.xml file.

No log mention an attempt to load a package-<mapping>.orm

--

Stephane


Andy
 

So if you're using JPA then you don't use that property ... since it is not mentioned in the JPA docs.

Create a new persistence unit per database. Just reference the "orm" file that is needed for that database in the persistence-unit. Not as good as the JDO way, but hey neither is the JPA spec also


stephane
 

It seems there are overriding rules, extending the JPA spec and I haven't seen it in DN doc.

I have a annotation in classes, a persistence.xml, an orm.xml and an orm-h2.xml.

When I load the persistence-unit with mapping-file orm-h2.xml, then the mapping is loaded as follow :

  1. Class annotation
  2. orm.xml
  3. orm-h2.xml

The orm.xml which is never mentioned (or I'm missing something) is loaded by default, before the one specified in the persistence-unit.

That's interesting, I can have in this orm.xml mapping file the default mapping, and override it for H2 in orm-h2.xml...

Thanks for you support.
--
Stephane


Andy
 
Edited

You mean the `orm.xml` that is specified in the JPA spec as the default ORM file? (and hence always processed if found, regardless of whether the user included it in persistence.xml). And then the other orm files that any user can put in their persistence.xml, again in the JPA spec. The JPA spec is not "extended" (or at least, this part of handling is not)


stephane
 

That's a little bit more tricky. 

I would say metadata are loaded as follow (you may know it already):
  1. class files and persistence.xml
  2. orm (ex: orm-h2.xml) file mentioned in the persistence.xml
  3. orm.xml
The overriding scope is at the class level. If the class is mentioned even partially, the mapping is totally overridden.




--
Stephane