DEV Community

Horia Constantin
Horia Constantin

Posted on • Originally published at horiaconstantin.com

Excluding Hibernate entities from auto-generation

Today I had to do something completely new with Hibernate, within the scope of a unit test. I had a group of entities and wanted to exclude one of them from the schema auto-generation (hibernate.hbm2ddl.auto=create).

After spending a couple of hours trying out different things and reading Stackoverflow answers, I hit jackpot with this cool solution:

  • implement the SchemaFilterProvider and the SchemaFilter interfaces
  • in the SchemaFilter implementation, add an if condition to includeTable so that it returns false for the table that you don’t want to create
  • add hibernate.properties to the classpath and define hibernate.hbm2ddl.schema_filter_provider to point to the SchemaFilterProvider implementation

What follows is an example of an implementation. In my case, I want to exclude all entities that would create a table that has namespace in its name.

Reference: https://stackoverflow.com/questions/6212144/how-to-disable-schema-validation-in-hibernate-for-certain-entities

Top comments (0)