Skip to content

Commit

Permalink
Hibernate5 backport PR#504 (#614)
Browse files Browse the repository at this point in the history
* Backport SecondaryTable support to liquibase5

* Remove settings.json

---------

Co-authored-by: Sayali Mohadikar <[email protected]>
  • Loading branch information
aplr and sayaliM0412 authored Nov 15, 2023
1 parent 0b7b52e commit 6b34450
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.Join;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -71,10 +72,13 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro

org.hibernate.mapping.Table hibernateTable = pc.getTable();
if (hibernateTable.isPhysicalTable()) {
Table table = new Table().setName(hibernateTable.getName());
table.setSchema(schema);
Scope.getCurrentScope().getLog(getClass()).info("Found table " + table.getName());
schema.addDatabaseObject(snapshotObject(table, snapshot));
addDatabaseObjectToSchema(hibernateTable, schema, snapshot);

Iterator<?> joins = pc.getJoinIterator();
while (joins.hasNext()) {
Join join = (Join) joins.next();
addDatabaseObjectToSchema(join.getTable(), schema, snapshot);
}
}
}

Expand All @@ -86,8 +90,6 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
IdentifierGenerator ig = persistentClass.getIdentifier().createIdentifierGenerator(
metadata.getIdentifierGeneratorFactory(),
database.getDialect(),
null,
null,
(RootClass) persistentClass
);
for (ExtendedSnapshotGenerator<IdentifierGenerator, Table> tableIdGenerator : tableIdGenerators) {
Expand All @@ -107,12 +109,16 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
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);
Scope.getCurrentScope().getLog(getClass()).info("Found table " + table.getName());
schema.addDatabaseObject(snapshotObject(table, snapshot));
addDatabaseObjectToSchema(hTable, schema, snapshot);
}
}
}
}

private void addDatabaseObjectToSchema(org.hibernate.mapping.Table join, Schema schema, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
Table joinTable = new Table().setName(join.getName());
joinTable.setSchema(schema);
Scope.getCurrentScope().getLog(getClass()).info("Found table " + joinTable.getName());
schema.addDatabaseObject(snapshotObject(joinTable, snapshot));
}
}
40 changes: 40 additions & 0 deletions src/test/java/com/example/ejb3/auction/FirstTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.ejb3.auction;

import javax.persistence.*;

@Entity
@SecondaryTable(name = "second_table", pkJoinColumns = @PrimaryKeyJoinColumn(name = "first_table_id"))
public class FirstTable {
@Id
private Long id;

@Column(name = "name")
private String name;

@Embedded
private SecondTable secondTable;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public SecondTable getSecondTable() {
return secondTable;
}

public void setSecondTable(SecondTable secondTable) {
this.secondTable = secondTable;
}
}
19 changes: 19 additions & 0 deletions src/test/java/com/example/ejb3/auction/SecondTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.ejb3.auction;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class SecondTable {

@Column(table = "second_table")
private String secondName;

public String getSecondName() {
return secondName;
}

public void setSecondName(String secondName) {
this.secondName = secondName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ public static void assertEjb3HibernateMapped(DatabaseSnapshot snapshot) {
hasProperty("name", is("AuditedItem")),
hasProperty("name", is("AuditedItem_AUD")),
hasProperty("name", is("REVINFO")),
hasProperty("name", is("WatcherSeqTable"))));
hasProperty("name", is("WatcherSeqTable")),
hasProperty("name", is("WatcherSeqTable")),
hasProperty("name", is("FirstTable")),
hasProperty("name", is("second_table"))));


Table bidTable = (Table) snapshot.get(new Table().setName("bid").setSchema(new Schema()));
Expand Down Expand Up @@ -90,5 +93,12 @@ public static void assertEjb3HibernateMapped(DatabaseSnapshot snapshot) {
hasProperty("primaryKeyTable", hasProperty("name", is("User")))
)
));

Table secondTable = (Table) snapshot.get(new Table().setName("second_table").setSchema(new Schema()));
assertThat(secondTable.getColumns(), containsInAnyOrder(
hasProperty("name", is("first_table_id")),
hasProperty("name", is("secondName"))
));
assertThat(secondTable.getPrimaryKey().getColumnNames(), is("first_table_id"));
}
}
1 change: 1 addition & 0 deletions src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<class>com.example.ejb3.auction.Watcher</class>
<class>com.example.ejb3.auction.Item</class>
<class>com.example.ejb3.auction.AuditedItem</class>
<class>com.example.ejb3.auction.FirstTable</class>
<properties>
<property name="hibernate.cache.use_second_level_cache" value="false"/>
<property name="hibernate.archive.autodetection" value="false"/>
Expand Down

0 comments on commit 6b34450

Please sign in to comment.