How to specify Hibernate configuration from Java rather than `hibernate.cfg.xml`

Hi,

We are using Hibernate, and for configuration we have Java code that calls Hibernate’s Configuration.addAnnotatedClass for each entity class.

I’m trying to figure out the best way to automatically generate schema changes whenever those entities’ definitions have changed.

Since we’re using this Java configuration instead of hibernate.cfg.xml, I was hoping there is some other way to specify the Hibernate configuration.

In the wiki, it mentions Home · liquibase/liquibase-hibernate Wiki · GitHub

The hibernate classic URL allows you to either reference your hibernate config XML file or specify a class name that implements liquibase/ext/hibernate/customfactory/CustomClassicConfigurationFactory

However, it looks like that was deleted: Support for hibernate 5 · liquibase/liquibase-hibernate@4926f52 · GitHub

Is there any replacement, or code that can accept an org.hibernate.SessionFactory or similar?

Hello @bancron,

When you say:

I take it you mean you are running a diffChangeLog like the below example right?

Run *liquibase --changeLogFile=changelog.xml --url=jdbc:yourdatabase --referenceUrl=hibernate:classic:path/to/hibernate.cfg.xml diffChangeLog*

However, instead of using hibernate.cfg.xml you want to use a Java configuration? Could you elaborate? Is the Java configuration an xml based file?

Sorry, I am not a Java developer but, I am trying to get more details out for those of us that know Liquibase better than Java :slight_smile:

Also, asking the maintainer may get you more traction, since the maintainer will be watching their extension probably closer than the forum or Discord:

Thanks,

Ronak

Yes, rather than having hibernate.cfg.xml, we have Java code to construct the org.hibernate.cfg.Configuration.

For example, instead of

        <property name = "hibernate.dialect">
            org.hibernate.dialect.PostgreSQLDialect
        </property>

        <property name = "hibernate.connection.driver_class">
            org.postgresql.Driver
        </property>

We have

configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
configuration.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");

and so forth.

Then we create a SessionFactory with the annotated classes.

ENTITY_CLASSES.forEach(config::addAnnotatedClass);
return config.buildSessionFactory();

It would be possible to move it all into cfg.xml files, and maybe that’s what I will have to do, but it would be great if we could just continue to specify the properties in Java as I’ve just described.

Some more context from the conversation on Discord:

More context:

bancron07/20/2020

it would be nice to continue to use hibernate to generate the schemas because we know they will be correct. it seems like doing the changes directly in liquibase could create a migration that does not exactly match the hibernate version.

[ 1:00 PM ]

that is why I was thinking we could just update the DB locally, then generate the diff between that and the existing liquibase schema

Okay, so I accomplished my goal by giving up on using hibernate integration.

I’m now doing everything through diffChangeLog.

The workflow looks like this:

  1. Drop the local database
  2. Re-initialize the local database with the new hibernate entities.
  3. Run diffChangeLog with referenceUrl=localhost, url=prod to generate myfile.postgresql.sql.
  4. Run update to update the target db.

Thus, the only changes in the changelog are going to be the most recent hibernate entities’ changes.

--liquibase formatted sql

--changeset bancron:1595536480516-1
ALTER TABLE myentity ADD mynewint INTEGER NOT NULL;