Support for various types of JPA generation for testing / production

I want to use GenerationType.IDENTITY for primary keys in my MySQL operating system. But for local development and testing, I would like to use HSQLDB. The problem is that HSQLDB does not support GenerationType.IDENTITY (at least with Eclipselink). I tried setting GenerationType.AUTO, which defaults to TABLE for HSQLDB, but unfortunately it does the same for MySQL, which I don't want. Is there a way to override this in persistence.xml? Is there any other trick I can use to make every environment do the right thing? I don’t have any entity configuration set to XML (this is all in the annotations), and I don’t want to change this, so I’m looking for a way to avoid this.

+4
source share
2 answers

The main idea here is to use a custom generator that will internally switch between the identifier and the table (or any other strategies that you need) based on the metadata information.

There is no way to do this using standard JPA. Although the @GeneratedValue annotation defines a generator parameter that allows you to specify a custom generator , it does not provide any mechanisms for writing one (only allowing you to use the built-in table / sequence generators).

It depends on the particular JPA provider (not) to implement this feature. The EclipseLink wiki has an example of how a custom generator can be defined. You will need to modify it to create internal instances of TableSequence / NativeSequence and switch between them based on session.getPlatform() .

Disclaimer: I have not tried using the above using EclipseLink; However, I did something very similar in Hibernate.

+2
source

HSQLDB obviously supports IDENTITY columns itself (specifically in v1.8), so this is a flaw in EclipseLink. For example, DataNucleus provides IDENTITY support for HSQLDB.

The job of using XML has its advantages when deployed using a cross-data warehouse, as you know for sure.

+1
source

All Articles