liquibase.ext.hibernate.database.JpaPersistenceDatabase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-hibernate4 Show documentation
Show all versions of liquibase-hibernate4 Show documentation
Liquibase extension for hibernate integration including generating changesets based on changed
hibernate mapping files
The newest version!
package liquibase.ext.hibernate.database;
import liquibase.database.DatabaseConnection;
import liquibase.exception.DatabaseException;
import liquibase.ext.hibernate.database.connection.HibernateConnection;
import liquibase.ext.hibernate.database.connection.HibernateDriver;
import org.hibernate.cfg.Configuration;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.service.ServiceRegistry;
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
import javax.persistence.spi.PersistenceUnitInfo;
import java.util.Collections;
/**
* Database implementation for JPA configurations.
* This supports passing a JPA persistence XML file reference.
*/
public class JpaPersistenceDatabase extends HibernateDatabase {
public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException {
return conn.getURL().startsWith("jpa:persistence:");
}
@Override
public String getDefaultDriver(String url) {
if (url.startsWith("jpa:persistence:")) {
return HibernateDriver.class.getName();
}
return null;
}
@Override
protected Configuration buildConfiguration(HibernateConnection connection) throws DatabaseException {
return buildConfigurationFromXml(connection);
}
/**
* Build a Configuration object assuming the connection path is a persistence XML configuration file.
*/
protected Configuration buildConfigurationFromXml(HibernateConnection connection) {
DefaultPersistenceUnitManager internalPersistenceUnitManager = new DefaultPersistenceUnitManager();
internalPersistenceUnitManager.setPersistenceXmlLocation(connection.getPath());
internalPersistenceUnitManager.setDefaultPersistenceUnitRootLocation(null);
internalPersistenceUnitManager.preparePersistenceUnitInfos();
PersistenceUnitInfo persistenceUnitInfo = internalPersistenceUnitManager.obtainDefaultPersistenceUnitInfo();
EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(persistenceUnitInfo,
Collections.emptyMap(), null);
ServiceRegistry serviceRegistry = builder.buildServiceRegistry();
return builder.buildHibernateConfiguration(serviceRegistry);
}
@Override
public String getShortName() {
return "jpaPersistence";
}
@Override
protected String getDefaultDatabaseProductName() {
return "JPA Persistence";
}
}