Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to new Hibernate 6.2 Generator API + Hibernate 6.3 breaking changes #508

Merged
merged 15 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
strategy:
fail-fast: false
matrix:
java: [ 17, 19 ]
java: [ 17, 20 ]

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
strategy:
fail-fast: false
matrix:
hibernate: [ "6.0.2.Final", "6.1.7.Final" ]
hibernate: [ "6.0.2.Final", "6.1.7.Final", "6.2.7.Final", "6.3.1.Final" ]

steps:
- uses: actions/checkout@v4
Expand Down
24 changes: 5 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>6.2.7.Final</hibernate.version>
<hibernate.version>6.3.1.Final</hibernate.version>
<spring.version>6.0.12</spring.version>
<liquibase.version>4.23.2</liquibase.version>
<!--suppress UnresolvedMavenProperty -->
Expand Down Expand Up @@ -211,7 +211,10 @@
<optimize>true</optimize>
<debug>true</debug>
<encoding>${project.build.sourceEncoding}</encoding>
<source>11</source>
<target>11</target>
</configuration>

</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
Expand All @@ -226,23 +229,6 @@
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.codehaus.gmavenplus</groupId>-->
<!-- <artifactId>gmavenplus-plugin</artifactId>-->
<!-- <version>1.13.1</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <goals>-->
<!-- <goal>addSources</goal>-->
<!-- <goal>addTestSources</goal>-->
<!-- <goal>compile</goal>-->
<!-- <goal>compileTests</goal>-->
<!-- <goal>removeStubs</goal>-->
<!-- <goal>removeTestStubs</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
Expand All @@ -256,7 +242,7 @@
<configuration>
<rules>
<requireJavaVersion>
<version>1.8</version>
<version>11</version>
</requireJavaVersion>
</rules>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.*;

import java.util.Iterator;

public class IndexSnapshotGenerator extends HibernateSnapshotGenerator {

private static final String HIBERNATE_ORDER_ASC = "asc";
Expand All @@ -25,27 +23,12 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
return example;
}
Relation table = ((Index) example).getRelation();
org.hibernate.mapping.Table hibernateTable = findHibernateTable(table, snapshot);
var hibernateTable = findHibernateTable(table, snapshot);
if (hibernateTable == null) {
return example;
}
Iterator<org.hibernate.mapping.Index> indexIterator = hibernateTable.getIndexIterator();
while (indexIterator.hasNext()) {
org.hibernate.mapping.Index hibernateIndex = indexIterator.next();
Index index = new Index();
index.setRelation(table);
index.setName(hibernateIndex.getName());
index.setUnique(isUniqueIndex(hibernateIndex));
Iterator<org.hibernate.mapping.Column> columnIterator = hibernateIndex.getColumnIterator();
while (columnIterator.hasNext()) {
org.hibernate.mapping.Column hibernateColumn = columnIterator.next();
String hibernateOrder = hibernateIndex.getColumnOrderMap().get(hibernateColumn);
Boolean descending = HIBERNATE_ORDER_ASC.equals(hibernateOrder)
? Boolean.FALSE
: (HIBERNATE_ORDER_DESC.equals(hibernateOrder) ? Boolean.TRUE : null);
index.getColumns().add(new Column(hibernateColumn.getName()).setRelation(table).setDescending(descending));
}

for (var hibernateIndex : hibernateTable.getIndexes().values()) {
Index index = handleHibernateIndex(table, hibernateIndex);
if (index.getColumnNames().equalsIgnoreCase(((Index) example).getColumnNames())) {
Scope.getCurrentScope().getLog(getClass()).info("Found index " + index.getName());
table.getIndexes().add(index);
Expand All @@ -67,35 +50,36 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
if (hibernateTable == null) {
return;
}
Iterator<org.hibernate.mapping.Index> indexIterator = hibernateTable.getIndexIterator();
while (indexIterator.hasNext()) {
org.hibernate.mapping.Index hibernateIndex = indexIterator.next();
Index index = new Index();
index.setRelation(table);
index.setName(hibernateIndex.getName());
index.setUnique(isUniqueIndex(hibernateIndex));
Iterator<org.hibernate.mapping.Column> columnIterator = hibernateIndex.getColumnIterator();
while (columnIterator.hasNext()) {
org.hibernate.mapping.Column hibernateColumn = columnIterator.next();
String hibernateOrder = hibernateIndex.getColumnOrderMap().get(hibernateColumn);
Boolean descending = HIBERNATE_ORDER_ASC.equals(hibernateOrder)
? Boolean.FALSE
: (HIBERNATE_ORDER_DESC.equals(hibernateOrder) ? Boolean.TRUE : null);
index.getColumns().add(new Column(hibernateColumn.getName()).setRelation(table).setDescending(descending));
}
for (var hibernateIndex : hibernateTable.getIndexes().values()) {
Index index = handleHibernateIndex(table, hibernateIndex);
Scope.getCurrentScope().getLog(getClass()).info("Found index " + index.getName());
table.getIndexes().add(index);
}
}
}

private Index handleHibernateIndex(Relation table, org.hibernate.mapping.Index hibernateIndex) {
Index index = new Index();
index.setRelation(table);
index.setName(hibernateIndex.getName());
index.setUnique(isUniqueIndex(hibernateIndex));
for (var hibernateColumn : hibernateIndex.getColumns()) {
String hibernateOrder = hibernateIndex.getColumnOrderMap().get(hibernateColumn);
Boolean descending = HIBERNATE_ORDER_ASC.equals(hibernateOrder)
? Boolean.FALSE
: (HIBERNATE_ORDER_DESC.equals(hibernateOrder) ? Boolean.TRUE : null);
index.getColumns().add(new Column(hibernateColumn.getName()).setRelation(table).setDescending(descending));
}
return index;
}

private Boolean isUniqueIndex(org.hibernate.mapping.Index hibernateIndex) {
/*
This seems to be necessary to explicitly tell liquibase that there's no
actual diff in certain non-unique indexes
*/
if (hibernateIndex.getColumnSpan() == 1) {
org.hibernate.mapping.Column col = hibernateIndex.getColumnIterator().next();
var col = hibernateIndex.getColumns().get(0);
return col.isUnique();
} else {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import liquibase.structure.core.Schema;
import liquibase.structure.core.Table;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.mapping.Join;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;
import org.hibernate.generator.Generator;
import org.hibernate.mapping.*;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -23,7 +21,7 @@

public class TableSnapshotGenerator extends HibernateSnapshotGenerator {

private List<ExtendedSnapshotGenerator<IdentifierGenerator, Table>> tableIdGenerators = new ArrayList<ExtendedSnapshotGenerator<IdentifierGenerator, Table>>();
private List<ExtendedSnapshotGenerator<Generator, Table>> tableIdGenerators = new ArrayList<>();

public TableSnapshotGenerator() {
super(Table.class, new Class[]{Schema.class});
Expand Down Expand Up @@ -83,15 +81,15 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro

Iterator<PersistentClass> classMappings = entityBindings.iterator();
while (classMappings.hasNext()) {
PersistentClass persistentClass = (PersistentClass) classMappings
.next();
if (!persistentClass.isInherited()) {
IdentifierGenerator ig = persistentClass.getIdentifier().createIdentifierGenerator(
PersistentClass persistentClass = classMappings.next();
if (!persistentClass.isInherited() && persistentClass.getIdentifier() instanceof SimpleValue) {
var simpleValue = (SimpleValue) persistentClass.getIdentifier();
Generator ig = simpleValue.createGenerator(
metadata.getMetadataBuildingOptions().getIdentifierGeneratorFactory(),
database.getDialect(),
(RootClass) persistentClass
);
for (ExtendedSnapshotGenerator<IdentifierGenerator, Table> tableIdGenerator : tableIdGenerators) {
for (ExtendedSnapshotGenerator<Generator, Table> tableIdGenerator : tableIdGenerators) {
if (tableIdGenerator.supports(ig)) {
Table idTable = tableIdGenerator.snapshot(ig);
idTable.setSchema(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,15 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
if (hibernateTable == null) {
return;
}
Iterator uniqueIterator = hibernateTable.getUniqueKeyIterator();
while (uniqueIterator.hasNext()) {
org.hibernate.mapping.UniqueKey hibernateUnique = (org.hibernate.mapping.UniqueKey) uniqueIterator.next();

for (var hibernateUnique : hibernateTable.getUniqueKeys().values()) {
UniqueConstraint uniqueConstraint = new UniqueConstraint();
uniqueConstraint.setName(hibernateUnique.getName());
uniqueConstraint.setRelation(table);
uniqueConstraint.setClustered(false); // No way to set true via Hibernate
Iterator columnIterator = hibernateUnique.getColumnIterator();

int i = 0;
while (columnIterator.hasNext()) {
org.hibernate.mapping.Column hibernateColumn = (org.hibernate.mapping.Column) columnIterator.next();
uniqueConstraint.addColumn(i, new Column(hibernateColumn.getName()).setRelation(table));
i++;
for (var hibernateColumn : hibernateUnique.getColumns()) {
uniqueConstraint.addColumn(i++, new Column(hibernateColumn.getName()).setRelation(table));
}

Index index = getBackingIndex(uniqueConstraint, hibernateTable, snapshot);
Expand All @@ -62,9 +57,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
Scope.getCurrentScope().getLog(getClass()).info("Found unique constraint " + uniqueConstraint.toString());
table.getUniqueConstraints().add(uniqueConstraint);
}
Iterator columnIterator = hibernateTable.getColumnIterator();
while (columnIterator.hasNext()) {
org.hibernate.mapping.Column column = (org.hibernate.mapping.Column) columnIterator.next();
for (var column : hibernateTable.getColumns()) {
if (column.isUnique()) {
UniqueConstraint uniqueConstraint = new UniqueConstraint();
uniqueConstraint.setRelation(table);
Expand All @@ -84,9 +77,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
}
}

Iterator<UniqueConstraint> ucIter = table.getUniqueConstraints().iterator();
while (ucIter.hasNext()) {
UniqueConstraint uc = ucIter.next();
for (UniqueConstraint uc : table.getUniqueConstraints()) {
if (uc.getName() == null || uc.getName().isEmpty()) {
String name = table.getName() + uc.getColumnNames();
name = "UCIDX" + hashedName(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import liquibase.structure.core.DataType;
import liquibase.structure.core.PrimaryKey;
import liquibase.structure.core.Table;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.generator.Generator;
import org.hibernate.id.enhanced.TableGenerator;

public class TableGeneratorSnapshotGenerator implements ExtendedSnapshotGenerator<IdentifierGenerator, Table> {
public class TableGeneratorSnapshotGenerator implements ExtendedSnapshotGenerator<Generator, Table> {

private static final String PK_DATA_TYPE = "varchar";
private static final String VALUE_DATA_TYPE = "bigint";

@Override
public Table snapshot(IdentifierGenerator ig) {
public Table snapshot(Generator ig) {
TableGenerator tableGenerator = (TableGenerator) ig;
Table table = new Table().setName(tableGenerator.getTableName());

Expand Down Expand Up @@ -44,7 +44,7 @@ public Table snapshot(IdentifierGenerator ig) {
}

@Override
public boolean supports(IdentifierGenerator ig) {
public boolean supports(Generator ig) {
return ig instanceof TableGenerator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashSet;
Expand All @@ -34,8 +35,7 @@
import java.util.Map.Entry;
import java.util.Set;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.*;

public class SpringPackageScanningIntegrationTest {
private static final String PACKAGES = "com.example.ejb3.auction";
Expand All @@ -52,7 +52,7 @@ public void setUp() throws Exception {
database = new HsqlDatabase();
database.setConnection(new JdbcConnection(connection));

Set<Class<? extends DatabaseObject>> typesToInclude = new HashSet<Class<? extends DatabaseObject>>();
Set<Class<? extends DatabaseObject>> typesToInclude = new HashSet<>();
typesToInclude.add(Table.class);
typesToInclude.add(Column.class);
typesToInclude.add(PrimaryKey.class);
Expand Down Expand Up @@ -95,16 +95,14 @@ public void runGeneratedChangeLog() throws Exception {

DiffResult diffResult = liquibase.diff(hibernateDatabase, database, compareControl);
boolean isTablePrefixWithZZ_ = diffResult.getMissingObjects().stream()
.filter(e -> e.getName().equals("zz_AuditedItem_AUD"))
.findAny()
.isPresent();
.anyMatch(e -> e.getName().equals("zz_AuditedItem_AUD"));
assertTrue(isTablePrefixWithZZ_);
assertTrue(diffResult.getMissingObjects().size() > 0);
assertFalse(diffResult.getMissingObjects().isEmpty());

File outFile = File.createTempFile("lb-test", ".xml");
OutputStream outChangeLog = new FileOutputStream(outFile);
String changeLogString = toChangeLog(diffResult);
outChangeLog.write(changeLogString.getBytes("UTF-8"));
outChangeLog.write(changeLogString.getBytes(StandardCharsets.UTF_8));
outChangeLog.close();

Scope.getCurrentScope().getLog(getClass()).info("Changelog:\n" + changeLogString);
Expand Down Expand Up @@ -287,21 +285,21 @@ private void hibernateSchemaExport(boolean enhancedId) throws Exception {

private String toString(DiffResult diffResult) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(out, true, "UTF-8");
PrintStream printStream = new PrintStream(out, true, StandardCharsets.UTF_8);
DiffToReport diffToReport = new DiffToReport(diffResult, printStream);
diffToReport.print();
printStream.close();
return out.toString("UTF-8");
return out.toString(StandardCharsets.UTF_8);
}

private String toChangeLog(DiffResult diffResult) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(out, true, "UTF-8");
PrintStream printStream = new PrintStream(out, true, StandardCharsets.UTF_8);
DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diffResult,
new DiffOutputControl().setIncludeCatalog(false).setIncludeSchema(false));
diffToChangeLog.print(printStream);
printStream.close();
return out.toString("UTF-8");
return out.toString(StandardCharsets.UTF_8);
}

private void ignoreDatabaseChangeLogTable(DiffResult diffResult)
Expand Down
Loading