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

Hibernate DDL created twice #38358

Merged
merged 1 commit into from
Jan 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ export class HibernateOrmPersistenceUnitsComponent extends LitElement {
</vaadin-details-summary>
<pre class="ddl-script">${pu.createDDL}</pre>
</vaadin-details>
<vaadin-details>
<vaadin-details-summary slot="summary" theme="filled">
<vaadin-horizontal-layout
theme="spacing"
style="align-items: center;">
<span>Update Script</span>
<vaadin-button @click="${(e) => this._copyToClipboard(e, 'Update Script')}"
theme="small">
<vaadin-icon icon="font-awesome-solid:clipboard"></vaadin-icon>
Copy
</vaadin-button>
</vaadin-horizontal-layout>
</vaadin-details-summary>
<pre class="ddl-script">${pu.updateDDL}</pre>
</vaadin-details>
<vaadin-details>
<vaadin-details-summary slot="summary" theme="filled">
<vaadin-horizontal-layout
Expand Down Expand Up @@ -114,4 +129,4 @@ export class HibernateOrmPersistenceUnitsComponent extends LitElement {
}

}
customElements.define('hibernate-orm-persistence-units', HibernateOrmPersistenceUnitsComponent);
customElements.define('hibernate-orm-persistence-units', HibernateOrmPersistenceUnitsComponent);
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import org.hibernate.boot.Metadata;
import org.hibernate.boot.query.NamedHqlQueryDefinition;
Expand All @@ -27,6 +28,7 @@
import org.hibernate.tool.schema.spi.SchemaDropper;
import org.hibernate.tool.schema.spi.SchemaManagementTool;
import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
import org.hibernate.tool.schema.spi.SchemaMigrator;
import org.hibernate.tool.schema.spi.ScriptSourceInput;
import org.hibernate.tool.schema.spi.ScriptTargetOutput;
import org.hibernate.tool.schema.spi.SourceDescriptor;
Expand Down Expand Up @@ -75,11 +77,32 @@ void pushPersistenceUnit(String persistenceUnitName,
}
}

String createDDL = generateDDL(Action.CREATE, metadata, serviceRegistry, importFile);
String dropDDL = generateDDL(Action.DROP, metadata, serviceRegistry, importFile);
DDLSupplier createDDLSupplier = new DDLSupplier(Action.CREATE, metadata, serviceRegistry, importFile);
DDLSupplier dropDDLSupplier = new DDLSupplier(Action.DROP, metadata, serviceRegistry, importFile);
DDLSupplier updateDDLSupplier = new DDLSupplier(Action.UPDATE, metadata, serviceRegistry, importFile);

info.add(new HibernateOrmDevInfo.PersistenceUnit(persistenceUnitName, managedEntities,
namedQueries, namedNativeQueries, createDDL, dropDDL));
namedQueries, namedNativeQueries, createDDLSupplier, dropDDLSupplier, updateDDLSupplier));
}

class DDLSupplier implements Supplier<String> {

private final Action action;
private final Metadata metadata;
private final ServiceRegistry serviceRegistry;
private final String importFile;

DDLSupplier(Action action, Metadata metadata, ServiceRegistry serviceRegistry, String importFile) {
this.action = action;
this.metadata = metadata;
this.serviceRegistry = serviceRegistry;
this.importFile = importFile;
}

@Override
public String get() {
return generateDDL(action, metadata, serviceRegistry, importFile);
}
}

void clearData() {
Expand Down Expand Up @@ -131,8 +154,11 @@ public void accept(String command) {
SchemaDropper schemaDropper = tool.getSchemaDropper(executionOptions.getConfigurationValues());
schemaDropper.doDrop(metadata, executionOptions, ContributableMatcher.ALL, source, target);
} else if (action == Action.CREATE) {
SchemaCreator schemaDropper = tool.getSchemaCreator(executionOptions.getConfigurationValues());
schemaDropper.doCreation(metadata, executionOptions, ContributableMatcher.ALL, source, target);
SchemaCreator schemaCreator = tool.getSchemaCreator(executionOptions.getConfigurationValues());
schemaCreator.doCreation(metadata, executionOptions, ContributableMatcher.ALL, source, target);
} else if (action == Action.UPDATE) {
SchemaMigrator schemaMigrator = tool.getSchemaMigrator(executionOptions.getConfigurationValues());
schemaMigrator.doMigration(metadata, executionOptions, ContributableMatcher.ALL, target);
}
return writer.toString();
} catch (RuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Supplier;

import org.hibernate.LockOptions;
import org.hibernate.boot.query.NamedHqlQueryDefinition;
Expand Down Expand Up @@ -44,18 +45,24 @@ public static class PersistenceUnit {
private final List<Entity> managedEntities;
private final List<Query> namedQueries;
private final List<Query> namedNativeQueries;
private final String createDDL;
private final String dropDDL;
private String createDDL;
private String dropDDL;
private String updateDDL;
private final Supplier<String> createDDLSupplier;
private final Supplier<String> dropDDLSupplier;
private final Supplier<String> updateDDLSupplier;

public PersistenceUnit(String name, List<Entity> managedEntities,
List<Query> namedQueries,
List<Query> namedNativeQueries, String createDDL, String dropDDL) {
List<Query> namedNativeQueries, Supplier<String> createDDL, Supplier<String> dropDDL,
Supplier<String> updateDDLSupplier) {
this.name = name;
this.managedEntities = managedEntities;
this.namedQueries = namedQueries;
this.namedNativeQueries = namedNativeQueries;
this.createDDL = createDDL;
this.dropDDL = dropDDL;
this.createDDLSupplier = createDDL;
this.dropDDLSupplier = dropDDL;
this.updateDDLSupplier = updateDDLSupplier;
}

public String getName() {
Expand All @@ -81,14 +88,27 @@ public List<Query> getAllNamedQueries() {
return allQueries;
}

public String getCreateDDL() {
public synchronized String getCreateDDL() {
if (createDDL == null) {
createDDL = createDDLSupplier.get();
}
return createDDL;
}

public String getDropDDL() {
public synchronized String getDropDDL() {
if (dropDDL == null) {
dropDDL = dropDDLSupplier.get();
}
return dropDDL;
}

public synchronized String getUpdateDDL() {
if (updateDDL == null) {
updateDDL = updateDDLSupplier.get();
}
return updateDDL;
}

}

public static class Entity {
Expand Down
Loading