Hi,
I’m new to Hibernate and first read the quickstart and introduction documents before I starting coding anything . So far, so good.
Now I’ve tried bootstrapping Hibernate via the following code:
var hibernateConfig = new org.hibernate.cfg.Configuration()
.setProperty(AvailableSettings.URL, props.dbUrl)
.setProperty(AvailableSettings.USER, props.dbUser)
.setProperty(AvailableSettings.PASS, props.dbPassword)
.setProperty(AvailableSettings.JAKARTA_HBM2DDL_CREATE_SCHEMAS,TRUE.toString())
.setProperty(AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, Action.ACTION_UPDATE);
Set<Class<?>> entityClasses = getEntityClasses();
entityClasses.forEach(hibernateConfig::addAnnotatedClass);
var sessionFactory = hibernateConfig.buildSessionFactory();
This works, the session factory is build and the database schema and tables are automatically created.
What puzzles me is that the first three setting parameters are marked as deprecated and that the JAKARTA_JDBC_* variants are now preferred. But when I use the latter ones, the code snippet above doesn’t work anymore; the session factory cannot be build and instead I see the following stack trace on my console:
(...)
2023-12-08 17:17:13,290 [XNIO-1 task-2] WARN org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator(:203) - HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
2023-12-08 17:18:33,976 [XNIO-1 task-2] WARN org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator(:345) - 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-6.4.0.Final.jar:6.4.0.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:428) ~[hibernate-core-6.4.0.Final.jar:6.4.0.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:61) ~[hibernate-core-6.4.0.Final.jar:6.4.0.Final]
(...)
It took me some hours to find out that the reason for this is to be found in
org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator#initiateService()
:
(...)
if ( connectionProvider == null ) {
if ( configurationValues.get( AvailableSettings.URL ) != null ) {
connectionProvider = new DriverManagerConnectionProviderImpl();
}
}
(...)
Shouldn’t the inner if statement not also check
configurationValues.get( AvailableSettings.JAKARTA_JDBC_URL ) != null
when that’s preferred according to the Javadoc? Actually it’s ignored, and one has to use deprecated settings for bootstrapping…
Or is there a better way for doing this?