liquibase.ext.hibernate.snapshot.TableSnapshotGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-hibernate5 Show documentation
Show all versions of liquibase-hibernate5 Show documentation
Liquibase extension for hibernate 5 integration including generating changesets based on changed
hibernate mapping files
The newest version!
package liquibase.ext.hibernate.snapshot;
/*
* #%L
* Liquibase Hibernate 5 Integration
* %%
* Copyright (C) 2016 Liquibase.org
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import liquibase.datatype.LiquibaseDataType;
import liquibase.exception.DatabaseException;
import liquibase.ext.hibernate.database.HibernateDatabase;
import liquibase.ext.hibernate.snapshot.extension.ExtendedSnapshotGenerator;
import liquibase.ext.hibernate.snapshot.extension.MultipleHiLoPerTableSnapshotGenerator;
import liquibase.ext.hibernate.snapshot.extension.TableGeneratorSnapshotGenerator;
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.snapshot.InvalidExampleException;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.*;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.hibernate.boot.spi.MetadataImplementor;
public class TableSnapshotGenerator extends HibernateSnapshotGenerator {
private List> tableIdGenerators
= new ArrayList>();
public TableSnapshotGenerator() {
super(Table.class, new Class[]{Schema.class});
tableIdGenerators.add(new MultipleHiLoPerTableSnapshotGenerator());
tableIdGenerators.add(new TableGeneratorSnapshotGenerator());
}
@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
if (example.getSnapshotId() != null) {
return example;
}
org.hibernate.mapping.Table hibernateTable = findHibernateTable(example, snapshot);
if (hibernateTable == null) {
return example;
}
Table table = new Table().setName(hibernateTable.getName());
LOG.info("Found table " + table.getName());
// table.setSnapshotId(SnapshotIdService.getInstance().generateId());
table.setSchema(example.getSchema());
return table;
}
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) {
return;
}
if (foundObject instanceof Schema) {
Schema schema = (Schema) foundObject;
HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase();
MetadataImplementor metadata = (MetadataImplementor) database.getMetadata();
Collection entityBindings = metadata.getEntityBindings();
Iterator tableMappings = entityBindings.iterator();
while (tableMappings.hasNext()) {
PersistentClass pc = tableMappings.next();
org.hibernate.mapping.Table hTable = pc.getTable();
if (hTable.isPhysicalTable()) {
Table table = new Table().setName(hTable.getName());
table.setSchema(schema);
LOG.info("Found table " + table.getName());
schema.addDatabaseObject(snapshotObject(table, snapshot));
}
}
tableMappings = entityBindings.iterator();
while (tableMappings.hasNext()) {
PersistentClass persistentClass = tableMappings.next();
if (!persistentClass.isInherited()) {
IdentifierGenerator ig = persistentClass.getIdentifier().createIdentifierGenerator(
metadata.getIdentifierGeneratorFactory(),
database.getDialect(),
null,
null,
(RootClass) persistentClass
);
for (ExtendedSnapshotGenerator tableIdGenerator : tableIdGenerators) {
if (tableIdGenerator.supports(ig)) {
Table idTable = tableIdGenerator.snapshot(ig);
idTable.setSchema(schema);
schema.addDatabaseObject(snapshotObject(idTable, snapshot));
break;
}
}
}
}
Collection collectionBindings = metadata.getCollectionBindings();
Iterator collIter = collectionBindings.iterator();
while (collIter.hasNext()) {
org.hibernate.mapping.Collection coll = collIter.next();
org.hibernate.mapping.Table hTable = coll.getCollectionTable();
if (hTable.isPhysicalTable()) {
Table table = new Table().setName(hTable.getName());
table.setSchema(schema);
LOG.info("Found table " + table.getName());
schema.addDatabaseObject(snapshotObject(table, snapshot));
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy