Skip to content

Commit

Permalink
Support for hibernate 5
Browse files Browse the repository at this point in the history
  • Loading branch information
nvoxland committed Aug 25, 2016
1 parent a620722 commit 4926f52
Show file tree
Hide file tree
Showing 20 changed files with 750 additions and 635 deletions.
30 changes: 10 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate4</artifactId>
<artifactId>liquibase-hibernate5</artifactId>
<version>3.6-SNAPSHOT</version>

<name>Liquibase Hibernate Integration</name>
Expand Down Expand Up @@ -68,7 +68,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>4.3.11.Final</hibernate.version>
<hibernate.version>5.2.2.Final</hibernate.version>
<spring.version>4.3.2.RELEASE</spring.version>
</properties>

<dependencies>
Expand All @@ -91,11 +92,6 @@
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
Expand All @@ -109,37 +105,31 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.4.RELEASE</version>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.4.RELEASE</version>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.4.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate</artifactId>
<version>1.2.9</version>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -173,8 +163,8 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.7</source>
<target>1.7</target>
<optimize>true</optimize>
<debug>true</debug>
<encoding>${project.build.sourceEncoding}</encoding>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import liquibase.ext.hibernate.database.HibernateDatabase;
import liquibase.ext.hibernate.database.connection.HibernateConnection;
import org.hibernate.cfg.Configuration;
import org.hibernate.boot.Metadata;

/**
* Implement this interface to dynamically generate a hibernate:ejb3 configuration.
* For example, if you create a class called com.example.hibernate.MyConfig, specify a url of hibernate:ejb3:com.example.hibernate.MyConfig.
*/
public interface CustomEjb3ConfigurationFactory {
public interface CustomMetadataFactory {

/**
/*
* Create a hibernate Configuration for the given database and connection.
*/
Configuration getConfiguration(HibernateDatabase hibernateDatabase, HibernateConnection connection);
Metadata getMetadata(HibernateDatabase hibernateDatabase, HibernateConnection connection);

}
}
Original file line number Diff line number Diff line change
@@ -1,71 +1,72 @@
package liquibase.ext.hibernate.database;

import liquibase.database.DatabaseConnection;
import liquibase.database.OfflineConnection;
import liquibase.exception.DatabaseException;
import liquibase.ext.hibernate.customfactory.CustomClassicConfigurationFactory;
import liquibase.ext.hibernate.database.connection.HibernateConnection;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.hibernate.service.ServiceRegistry;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Properties;

/**
* Database implementation for "classic" hibernate configurations.
* This supports passing a hibernate xml configuration file or a {@link CustomClassicConfigurationFactory} implementation
*/
public class HibernateClassicDatabase extends HibernateDatabase {

protected Configuration configuration;

public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException {
return conn.getURL().startsWith("hibernate:classic:");
}

@Override
protected Configuration buildConfiguration(HibernateConnection connection) throws DatabaseException {
protected String findDialectName() {
String dialectName = super.findDialectName();

if (isCustomFactoryClass(connection.getPath())) {
return buildConfigurationFromFactory(connection);
} else {
return buildConfigurationfromFile(connection);
if (dialectName == null) {
dialectName = configuration.getProperty(AvailableSettings.DIALECT);
}
return dialectName;
}

/**
* Build a Configuration object assuming the connection path is a {@link CustomClassicConfigurationFactory} class name
*/
protected Configuration buildConfigurationFromFactory(HibernateConnection connection) throws DatabaseException {
try {
return ((CustomClassicConfigurationFactory) Class.forName(connection.getPath()).newInstance()).getConfiguration(this, connection);
} catch (InstantiationException e) {
throw new DatabaseException(e);
} catch (IllegalAccessException e) {
throw new DatabaseException(e);
} catch (ClassNotFoundException e) {
throw new DatabaseException(e);
}
}

/**
* Build a Configuration object assuming the connection path is a hibernate XML configuration file.
*/
protected Configuration buildConfigurationfromFile(HibernateConnection connection) {
Configuration configuration = new Configuration();
configuration.configure(connection.getPath());
configureNamingStrategy(configuration, connection);
return configuration;
protected Metadata generateMetadata() throws DatabaseException {
this.configuration = new Configuration();
this.configuration.configure(getHibernateConnection().getPath());

return super.generateMetadata();
}

/**
* Returns true if the given path is a factory class
*/
protected boolean isCustomFactoryClass(String path) {
if (path.contains("/")) {
return false;
}
@Override
protected void addToSources(MetadataSources sources) throws DatabaseException {
Configuration config = new Configuration(sources);
config.configure(getHibernateConnection().getPath());

try {
Class<?> clazz = Class.forName(path);
return CustomClassicConfigurationFactory.class.isAssignableFrom(clazz);
} catch (ClassNotFoundException e) {
return false;
}
config.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");

This comment has been minimized.

Copy link
@jonenst

jonenst Oct 9, 2023

Contributor

hi @nvoxland do you remember why you set this only for HibernateClassicDatabase? It seems needed for other cases as well (for example but not limited to HibernateSpringPackageDatabase, see #431 ) (and variants : #482 #481 # 381 ...) ?

maybe add the same line " config.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");" to HibernateSpringPackageDatabase or maybe Ejb3Database so that all children classes using createEntityManagerFactoryBuilder benefit from it ?
(see
https://docs.jboss.org/hibernate/orm/5.6/userguide/html_single/Hibernate_User_Guide.html#configurations-internal
https://docs.jboss.org/hibernate/orm/6.1/userguide/html_single/Hibernate_User_Guide.html#configurations-internal
Note: no longer documented in hibernate 6.2/6.3 after the switch to documentation generated from AvailableSettings.java (hibernate/hibernate-orm@fd961cebb10 ) which doesn't have it
)

Or just catch the exception to avoid confusing users ?

maybe @filipelautert , @papegaaij, @DanielFran can chime in as well ?

This comment has been minimized.

Copy link
@filipelautert

filipelautert Oct 11, 2023

Collaborator

@jonenst I've no idea why it was not included, but the plan of catching the exception would be interesting! as if we add the parameter now it could change behavior to people that uses the extension, right?

This comment has been minimized.

Copy link
@jonenst

jonenst Oct 16, 2023

Contributor

hi @filipelautert , thanks for you answer and your time. I'm not sure about all the effects of adding the parameter, maybe someone with more knowledge about hibernate can provide information. But maybe it's still the right thing to do ?
"This setting is used to control whether we should consult the JDBC metadata to determine certain Settings default values when the database may not be available (mainly in tools usage)."
Looks like it could be a legitmate use of the parameters because it's intended for tools usage.
Also it was included in ":classic" so maybe include it everywhere and people adapt if they see unforseen effects ? but at least everything becomes more homogeneous ?
Catching an exception is frequently a bad sign.

This comment has been minimized.

Copy link
@jonenst

jonenst Oct 18, 2023

Contributor

Note, here are runs from April 2022: the warning was already there but was only 2 lines instead of 80+

[INFO] Liquibase Community 3.10.3 by Datical
[INFO] HHH000412: Hibernate Core {5.4.4.Final}
[INFO] HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
[WARNING] HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
[WARNING] HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
[INFO] HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
[INFO] Liquibase Community 4.5.0 by Datical
[INFO] HHH000412: Hibernate ORM core version 5.5.6
[INFO] HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
[WARNING] HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
[WARNING] 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)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection (JdbcEnvironmentInitiator.java:181)
...
80 lines stacktrace
...
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[INFO] HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect

This comment has been minimized.

Copy link
@filipelautert

filipelautert Oct 18, 2023

Collaborator

@jonenst I think I already faced this annoying error message! Would you mind creating an issue or (even better) a PR so we can handle that?

This comment has been minimized.

Copy link
@jonenst

jonenst Nov 9, 2023

Contributor

Hi @filipelautert , I've made a PR #616
What do you think ?

there were already many issues as I said in my first message:
see #431 ) (and variants : #482 #481 #482 #381 , maybe others...) ?



ServiceRegistry standardRegistry = configuration.getStandardServiceRegistryBuilder()
.applySettings(config.getProperties())
.addService(ConnectionProvider.class, new NoOpConnectionProvider())
.addService(MultiTenantConnectionProvider.class, new NoOpConnectionProvider())
.build();

config.buildSessionFactory(standardRegistry);
}

@Override
Expand All @@ -79,5 +80,4 @@ protected String getDefaultDatabaseProductName() {
}



}
Loading

0 comments on commit 4926f52

Please sign in to comment.