diff --git a/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java b/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java index c11e4eb3..fce59612 100644 --- a/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java +++ b/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java @@ -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; @@ -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); + } } } @@ -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 tableIdGenerator : tableIdGenerators) { @@ -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)); + } } diff --git a/src/test/java/com/example/ejb3/auction/FirstTable.java b/src/test/java/com/example/ejb3/auction/FirstTable.java new file mode 100644 index 00000000..04aa8b88 --- /dev/null +++ b/src/test/java/com/example/ejb3/auction/FirstTable.java @@ -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; + } +} \ No newline at end of file diff --git a/src/test/java/com/example/ejb3/auction/SecondTable.java b/src/test/java/com/example/ejb3/auction/SecondTable.java new file mode 100644 index 00000000..ef3bd4fd --- /dev/null +++ b/src/test/java/com/example/ejb3/auction/SecondTable.java @@ -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; + } +} \ No newline at end of file diff --git a/src/test/java/liquibase/ext/hibernate/database/HibernateEjb3DatabaseTest.java b/src/test/java/liquibase/ext/hibernate/database/HibernateEjb3DatabaseTest.java index 122963bd..2f7b1004 100644 --- a/src/test/java/liquibase/ext/hibernate/database/HibernateEjb3DatabaseTest.java +++ b/src/test/java/liquibase/ext/hibernate/database/HibernateEjb3DatabaseTest.java @@ -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())); @@ -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")); } } diff --git a/src/test/resources/META-INF/persistence.xml b/src/test/resources/META-INF/persistence.xml index 99069fad..57a1e34b 100644 --- a/src/test/resources/META-INF/persistence.xml +++ b/src/test/resources/META-INF/persistence.xml @@ -12,6 +12,7 @@ com.example.ejb3.auction.Watcher com.example.ejb3.auction.Item com.example.ejb3.auction.AuditedItem + com.example.ejb3.auction.FirstTable