# Generating changelog programmatically: The application must supply JDBC connections

**URL:** <https://forum.liquibase.org/t/generating-changelog-programmatically-the-application-must-supply-jdbc-connections/5729>\
**Category:** General Discussion\
**Created:** [July 23, 2021, 4:19pm UTC](https://forum.liquibase.org/t/generating-changelog-programmatically-the-application-must-supply-jdbc-connections/5729 "2021-07-23T16:19:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lnthai2002](https://avatars.discourse-cdn.com/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://forum.liquibase.org/u/lnthai2002)\
**Post date:** [July 23, 2021, 4:19pm UTC](https://forum.liquibase.org/t/generating-changelog-programmatically-the-application-must-supply-jdbc-connections/5729/1 "2021-07-23T16:19:20Z")

</div>

Hello, I have the following code to generate changelog programmatically:

```auto
      Database db = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
      
      String url = "hibernate:spring:cloud.service2,cloud.boot.starter.data"
      + "?dialect=org.hibernate.dialect.MySQL8Dialect"
      + "&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy"
      + "&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy";
      
      HibernateConnection hibernateConnection = new HibernateConnection(url, null);
      JdbcConnection jdbcConnection = new JdbcConnection(hibernateConnection);
      Database hdb = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);

      File changelog = new File(CHANGELOG_FOLDER + File.separator + changelogFileFileName);
      changelog.delete();
      CommandLineUtils.doDiffToChangeLog(changelog.getAbsolutePath(), hdb, db, new DiffOutputControl(false, false, false, null), null, null);

      return Optional.of(changelog);

```

however, during the building of HibernateSpringPakcageDatabase (hdb in the code above) when the execution get to buildMetadataFromPath() of HibernateEjb3Database it throw an error at building the EntityManagerFactory:

```auto
2021-07-23 10:41:06.023 WARN 28764 [main] o.h.e.j.e.i.JdbcEnvironmentInitiator * * * : HHH000342: Could not obtain connection to query metadata

java.lang.UnsupportedOperationException: The application must supply JDBC connections
	at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:44) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:176) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:127) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
	at liquibase.ext.hibernate.database.HibernateEjb3Database.buildMetadataFromPath(HibernateEjb3Database.java:59) ~[liquibase-hibernate5-4.3.5.jar:na]
	at liquibase.ext.hibernate.database.HibernateDatabase.buildMetadata(HibernateDatabase.java:143) ~[liquibase-hibernate5-4.3.5.jar:na]
	at liquibase.ext.hibernate.database.HibernateDatabase.setConnection(HibernateDatabase.java:83) ~[liquibase-hibernate5-4.3.5.jar:na]
	at liquibase.database.DatabaseFactory.findCorrectDatabaseImplementation(DatabaseFactory.java:121) ~[liquibase-core-4.3.5.jar:na]

```

My understanding is that the HibernateSpringPackageDatabase is a meta database without real connection, however, at some point Hibernate is trying to get its connection. The root cause of this issue is that the buildMetadata() of HibernateDatabase class use the path from the url i specified to load the class but the path is the package, not a classname so it failed and thus fall back to loading metadata using connection.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/liquibase1/original/1X/d3e1f02040ee503baf0d68b872a0e1afe75afc94.jpeg)

My question has 2 parts:  
1/My url match the template for HibernateSpringPackageDatabase which mean there are multiple Entity classes in the package, why the build metadata expects only 1 class? Am I wrong assuming that the changelog can be generated by diff 2 database with more than one entity/table?  
2/Is there a documentation of how to use liquibase programmatically?

---

<div class="post-metadata">

**Author:** ![rakhi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.liquibase.org/rakhi/32/288_2.png) [@rakhi](https://forum.liquibase.org/u/rakhi)\
**Post date:** [July 26, 2021, 1:44pm UTC](https://forum.liquibase.org/t/generating-changelog-programmatically-the-application-must-supply-jdbc-connections/5729/2 "2021-07-26T13:44:22Z")

</div>

Hi @lnthai2002

> [@lnthai2002](#):
>
> 2/Is there a documentation of how to use liquibase programmatically?

Are you looking for Liquibase Java API documentation? Please have a look if [this](https://www.liquibase.org/javadoc/) could help in some way.

Thanks,  
Rakhi Agrawal

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://avatars.discourse-cdn.com/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://forum.liquibase.org/u/lnthai2002)\
**Post date:** [July 26, 2021, 3:15pm UTC](https://forum.liquibase.org/t/generating-changelog-programmatically-the-application-must-supply-jdbc-connections/5729/3 "2021-07-26T15:15:19Z")

</div>

Thank you Rakhi, I saw the javadoc but they don’t help much. I am looking for some guide line, for example, to generate change log, I need 2 schemas to compare, one is the current state of the DB (current) and the other is the the state after the changes are applied (target); which class I should use to get the schema of the target?

---

<div class="post-metadata">

**Author:** ![rakhi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.liquibase.org/rakhi/32/288_2.png) [@rakhi](https://forum.liquibase.org/u/rakhi)\
**Post date:** [July 28, 2021, 12:07pm UTC](https://forum.liquibase.org/t/generating-changelog-programmatically-the-application-must-supply-jdbc-connections/5729/4 "2021-07-28T12:07:09Z")

</div>

umm, please bear with my questions. I’m a bit lost with your last comment.  
Are you trying to generate a diff changelog here? OR Are you tring to generate a changelog of already created DB (in final state)?

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://avatars.discourse-cdn.com/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://forum.liquibase.org/u/lnthai2002)\
**Post date:** [July 28, 2021, 1:40pm UTC](https://forum.liquibase.org/t/generating-changelog-programmatically-the-application-must-supply-jdbc-connections/5729/5 "2021-07-28T13:40:40Z")

</div>

Thank you Rakhi. The intention is to generate the diff changelog between the current database and the entity set. We are doing this because when new Entities are added to the code, we need to generate the diff changelog then verify that it does not drop anything already exist in the current DB before applying the changelog to the DB.

---

<div class="post-metadata">

**Author:** ![rakhi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.liquibase.org/rakhi/32/288_2.png) [@rakhi](https://forum.liquibase.org/u/rakhi)\
**Post date:** [July 29, 2021, 9:54am UTC](https://forum.liquibase.org/t/generating-changelog-programmatically-the-application-must-supply-jdbc-connections/5729/6 "2021-07-29T09:54:19Z")

</div>

ohh I see. So may be you are looking for something like this [here](https://www.liquibase.org/blog/comparing-two-states-database-schema).

I hope this gives you some idea. The way explained on this link is not programmatically, but same should be implemented through liquibase API as well.

Thanks!  
Rakhi Agarwal

---

<div class="post-metadata">

**Author:** ![larue.thierry](https://avatars.discourse-cdn.com/v4/letter/l/76d3ee/32.png) [@larue.thierry](https://forum.liquibase.org/u/larue.thierry)\
**Post date:** [April 24, 2023, 8:19am UTC](https://forum.liquibase.org/t/generating-changelog-programmatically-the-application-must-supply-jdbc-connections/5729/7 "2023-04-24T08:19:13Z")

</div>

Hello, I have exactly the same problem, with this configuration (last version) :

Springboot 3.0.5  
Hibernate 6  
Liquibase Maven Plugin 4.21.1

I want to generate diff between JPA entity and database postgresql.  
When I debug liquibase, I see that the good implementation is finded, (HibernateSpringPakcageDatabase) but it try to set connection ☹

Have you a solution for this case please ?

 ![image](https://us1.discourse-cdn.com/flex020/uploads/liquibase1/original/1X/b0d5f8f16c00d7f45171d92010e2c5bdf38cdc63.png)
