Is it possible to replace Hibernate @ForeignKey annotations on a CLASS with a JPA-style ForeignKey annotation?


#1

Removing old org.hibernate.annotations.ForeignKey annontations from our code since its deprecated. Succeeded aside from two remaining annontations. These to are currently place on a class instead of a member field.

// class FK nr 1
@Entity
@org.hibernate.annotations.ForeignKey(name = "FK_DynamicPageMenuDisplayConfiguration_DynamicPageDisplayConfiguration")
public class DynamicPageMenuDisplayConfiguration extends DynamicPageDisplayConfiguration {

// class FK nr 2
@Entity
@org.hibernate.annotations.ForeignKey(name = "FK_DynamicPageCatalogDisplayConfiguration_DynamicPageDisplayConfiguration")
public class DynamicPageCatalogDisplayConfiguration extends DynamicPageDisplayConfiguration {

Both extend DynamicPageDisplayConfiguration.

// File DynamicPageDisplayConfiguration
Entity
@Inheritance(strategy = InheritanceType.JOINED)
public **abstract** class **DynamicPageDisplayConfiguration** {

Is it possible to replace the Hibernate FK annontation for a JPA style FK annontation in this case?


#2

The JPA @ForeignKey annotation cannot be applied to entity classes like the Hibernate one. But you can use the @PrimaryKeyJoinColumn annotation and set the foreignKey attribute:

@Entity
@PrimaryKeyJoinColumn(name = "parent_id", foreignKey = @ForeignKey(name = "FK_DynamicPageMenuDisplayConfiguration_DynamicPageDisplayConfiguration")
public class DynamicPageMenuDisplayConfiguration extends DynamicPageDisplayConfiguration {

// class FK nr 2
@Entity
@org.hibernate.annotations.ForeignKey(name = "FK_DynamicPageCatalogDisplayConfiguration_DynamicPageDisplayConfiguration")
public class DynamicPageCatalogDisplayConfiguration extends DynamicPageDisplayConfiguration {

Anyway, it’s not a very good idea to rely on hbm2ddl to generate the DB schema. You should use incremental migration scripts and a tool like Flyway instead.

@Entity
@PrimaryKeyJoinColumn(
	name = "parent_id", 
	foreignKey = @ForeignKey(
		name = "FK_DynamicPageMenuDisplayConfiguration_DynamicPageDisplayConfiguration"
	)
)
public class DynamicPageMenuDisplayConfiguration extends DynamicPageDisplayConfiguration {

// class FK nr 2
@Entity
@PrimaryKeyJoinColumn(
	name = "parent_id", 
	foreignKey = @ForeignKey(
		name = "FK_DynamicPageCatalogDisplayConfiguration_DynamicPageDisplayConfiguration"
	)
)
public class DynamicPageCatalogDisplayConfiguration extends DynamicPageDisplayConfiguration {

#3

Thanks for the very fast response. Works like a charm. Now all ForeignKey annontations are JPA style.

Wrt your remark: we are using something similar to Flyway for managing our database. Its liquibase (CSV for databases). For our unit tests we use a H2 database for which a certain database schema is build on the fly for a certain test(s). For this I assume we use hbm2ddl to generate the DB schema - setup long long ago. At that moment the Hibernate annontations like ForeignKey are used.

And yes its a PITA to get our liquibase and the Hibernate annontations in sync. Not sure if there is a simple way to find out if everything is in sync. Already found multiple FK’s in the code, which are not in liquibase. Liquibase is leading and used when running the web application.


#4

Wrt your remark: we are using something similar to Flyway for managing our database. Its liquibase (CSV for databases).

That’s good. Liquibase is also an automatic migration script tool too.

For our unit tests we use a H2 database for which a certain database schema is build on the fly for a certain test(s).

You should use the same DB schema as the one used in production. More, you need to test against the same DB engine you use in production. Use Docker and tmpfs too speed up tests so that they run almost as fast as H2.

For this I assume we use hbm2ddl to generate the DB schema - setup long long ago. At that moment the Hibernate annontations like ForeignKey are used.

You can use the hbm2ddl tool to generate the initial script or to periodically generate some scripts that you use to create the actual migration scripts.


#5

We are going to have a look (hopefully in the near future) how we can use unit tests in combination with liquibase and MS SQL server and migrate from our current setup. Thanks for all the help. This ticket can be closed.