Skip to content

Commit

Permalink
HBX-2880: Create and use subclasses for BasicValue and Component to a…
Browse files Browse the repository at this point in the history
…ccommodate for changes in identity generation

Signed-off-by: Koen Aers <koen.aers@gmail.com>
  • Loading branch information
koentsje committed Jul 29, 2024
1 parent a8e817d commit 2895ada
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.hibernate.persister.spi.PersisterClassResolver;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.internal.export.common.EntityNameFromValueVisitor;
import org.hibernate.tool.internal.reveng.util.EnhancedValue;
import org.hibernate.tool.internal.util.SkipBackRefPropertyIterator;

/**
Expand Down Expand Up @@ -180,11 +181,20 @@ public boolean isIdentifierGeneratorProperties(Property property) {
}

public Properties getIdentifierGeneratorProperties(Property property) {
Properties result = null;
SimpleValue simpleValue = (SimpleValue)property.getValue();
Properties result = new Properties();
Map<String, Object> idGenParams = simpleValue.getIdentifierGeneratorParameters();
if (idGenParams != null) {
result.putAll(idGenParams);
if (simpleValue instanceof EnhancedValue) {
Properties idGenParams = ((EnhancedValue)simpleValue).getIdentifierGeneratorProperties();
if (idGenParams != null) {
result = new Properties();
result.putAll(idGenParams);
}
} else {
Map<String, Object> properties = simpleValue.getIdentifierGeneratorParameters();
if (properties != null) {
result = new Properties();
result.putAll(properties);
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Value;
import org.hibernate.tool.internal.export.hbm.Cfg2HbmTool;
import org.hibernate.tool.internal.reveng.util.EnhancedValue;
import org.hibernate.tool.internal.util.NameConverter;
import org.hibernate.tool.internal.util.StringUtil;
import org.hibernate.type.Type;
Expand Down Expand Up @@ -262,7 +263,7 @@ public String getGenericCollectionDeclaration(Collection collection, boolean pre
* @return
*/
public Properties getFilteredIdentifierGeneratorProperties(SimpleValue simpleValue) {
Properties p = simpleValue.getIdentifierGeneratorProperties();
Properties p = ((EnhancedValue)simpleValue).getIdentifierGeneratorProperties();
return Cfg2HbmTool.getFilteredIdentifierGeneratorProperties(p, new Properties());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.hibernate.mapping.ToOne;
import org.hibernate.mapping.UniqueKey;
import org.hibernate.mapping.Value;
import org.hibernate.tool.internal.reveng.util.EnhancedBasicValue;
import org.hibernate.tool.internal.reveng.util.EnhancedValue;
import org.hibernate.tool.internal.util.AnnotationBuilder;
import org.hibernate.tool.internal.util.IteratorTransformer;
import org.hibernate.tool.internal.util.SkipBackRefPropertyIterator;
Expand Down Expand Up @@ -220,10 +222,10 @@ public String generateAnnIdGenerator() {

wholeString.append( AnnotationBuilder.createAnnotation( importType("jakarta.persistence.EmbeddedId") ).getResult());
}
else if ( identifier instanceof SimpleValue ) {
SimpleValue simpleValue = (SimpleValue) identifier;
strategy = simpleValue.getIdentifierGeneratorStrategy();
properties = c2j.getFilteredIdentifierGeneratorProperties(simpleValue);
else if ( identifier instanceof EnhancedBasicValue ) {
EnhancedBasicValue enhancedBasicValue = (EnhancedBasicValue) identifier;
strategy = enhancedBasicValue.getIdentifierGeneratorStrategy();
properties = c2j.getFilteredIdentifierGeneratorProperties(enhancedBasicValue);
StringBuffer idResult = new StringBuffer();
AnnotationBuilder builder = AnnotationBuilder.createAnnotation( importType("jakarta.persistence.Id") );
idResult.append(builder.getResult());
Expand Down Expand Up @@ -865,11 +867,15 @@ protected List<Property> getPropertiesForMinimalConstructor(PersistentClass pc)

protected boolean isAssignedIdentifier(PersistentClass pc, Property property) {
if(property.equals(pc.getIdentifierProperty())) {
if(property.getValue().isSimpleValue()) {
SimpleValue sv = (SimpleValue) property.getValue();
if(property.getValue() instanceof EnhancedValue) {
EnhancedValue sv = (EnhancedValue) property.getValue();
if("assigned".equals(sv.getIdentifierGeneratorStrategy())) {
return true;
}
} else if (property.getValue().isSimpleValue()) {
if("assigned".equals(((SimpleValue)property.getValue()).getIdentifierGeneratorStrategy())) {
return true;
}
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.hibernate.tool.internal.reveng.binder;

import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table;
import org.hibernate.tool.internal.reveng.util.EnhancedBasicValue;

class BasicValueBinder extends AbstractBinder {

Expand All @@ -15,11 +14,11 @@ private BasicValueBinder(BinderContext binderContext) {
super(binderContext);
}

SimpleValue bind(
EnhancedBasicValue bind(
Table table,
Column column,
boolean generatedIdentifier) {
SimpleValue value = new BasicValue(getMetadataBuildingContext(), table);
EnhancedBasicValue value = new EnhancedBasicValue(getMetadataBuildingContext(), table);
value.addColumn(column);
value.setTypeName(TypeUtils.determinePreferredType(
getMetadataCollector(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table;
import org.hibernate.tool.api.reveng.TableIdentifier;
import org.hibernate.tool.internal.reveng.RevengMetadataCollector;
import org.hibernate.tool.internal.reveng.binder.ForeignKeyUtils.ForeignKeyForColumns;
import org.hibernate.tool.internal.reveng.util.EnhancedBasicValue;
import org.hibernate.tool.internal.reveng.util.EnhancedComponent;
import org.hibernate.tool.internal.reveng.util.EnhancedValue;
import org.hibernate.tool.internal.reveng.util.RevengUtils;

class PrimaryKeyBinder extends AbstractBinder {
Expand Down Expand Up @@ -56,7 +58,7 @@ PrimaryKeyInfo bind(
List<Column> keyColumns = getKeyColumns(table);
final TableIdentifier tableIdentifier = TableIdentifier.create(table);
PrimaryKeyInfo pki = createPrimaryKeyInfo(tableIdentifier, keyColumns);
SimpleValue id = createKeyValue(rc, keyColumns, pki.suggestedStrategy, table, revengMetadataCollector, processed);
EnhancedValue id = createKeyValue(rc, keyColumns, pki.suggestedStrategy, table, revengMetadataCollector, processed);
id.setIdentifierGeneratorProperties(pki.suggestedProperties);
Property property = propertyBinder.bind(
table,
Expand All @@ -70,7 +72,7 @@ PrimaryKeyInfo bind(
}

void updatePrimaryKey(RootClass rc, PrimaryKeyInfo pki) {
SimpleValue idValue = (SimpleValue) rc.getIdentifierProperty().getValue();
EnhancedValue idValue = (EnhancedValue) rc.getIdentifierProperty().getValue();
Properties defaultStrategyProperties = new Properties();
Property constrainedOneToOne = getConstrainedOneToOne(rc);
if(constrainedOneToOne!=null) {
Expand All @@ -84,7 +86,7 @@ void updatePrimaryKey(RootClass rc, PrimaryKeyInfo pki) {
}
}

private SimpleValue createKeyValue(
private EnhancedValue createKeyValue(
PersistentClass rc,
List<Column> keyColumns,
String suggestedStrategyName,
Expand Down Expand Up @@ -174,27 +176,27 @@ private List<Column> getKeyColumns(Table table) {
return result;
}

private SimpleValue handleColumnKey(
private EnhancedBasicValue handleColumnKey(
Table table,
String tableIdentifierStrategyName,
Set<Column> processed,
List<Column> keyColumns) {
Column pkc = (Column) keyColumns.get(0);
BinderUtils.checkColumnForMultipleBinding(pkc);
processed.add(pkc);
SimpleValue result = simpleValueBinder.bind(
EnhancedBasicValue result = simpleValueBinder.bind(
table,
pkc,
isGeneratedId(keyColumns, tableIdentifierStrategyName));
result.setIdentifierGeneratorStrategy(tableIdentifierStrategyName);
return result;
}

private SimpleValue handleCompositeKey(
private EnhancedComponent handleCompositeKey(
PersistentClass rc,
Set<Column> processedColumns,
List<Column> keyColumns) {
Component result = new Component(getMetadataBuildingContext(), rc);
EnhancedComponent result = new EnhancedComponent(getMetadataBuildingContext(), rc);
result.setMetaAttributes(Collections.EMPTY_MAP);
result.setEmbedded(false);
result.setComponentClassName(getCompositeIdName(rc));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.hibernate.tool.internal.reveng.util;

import java.util.Properties;

import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Table;

@SuppressWarnings("serial")
public class EnhancedBasicValue extends BasicValue implements EnhancedValue {

private Properties idGenProps = new Properties();
private String genStrategy = null;

public EnhancedBasicValue(MetadataBuildingContext buildingContext, Table table) {
super(buildingContext, table);
}

@Override
public void setIdentifierGeneratorProperties(Properties props) {
idGenProps = props;

}

@Override
public Properties getIdentifierGeneratorProperties() {
return idGenProps;
}

@Override
public void setIdentifierGeneratorStrategy(String s) {
genStrategy = s;
}

@Override
public String getIdentifierGeneratorStrategy() {
return genStrategy;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.hibernate.tool.internal.reveng.util;

import java.util.Properties;

import org.hibernate.MappingException;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.PersistentClass;

@SuppressWarnings("serial")
public class EnhancedComponent extends Component implements EnhancedValue {

private Properties idGenProps = new Properties();
private String genStrategy = null;

public EnhancedComponent(MetadataBuildingContext metadata, PersistentClass owner) throws MappingException {
super(metadata, owner);
}

@Override
public void setIdentifierGeneratorProperties(Properties props) {
idGenProps = props;

}

@Override
public Properties getIdentifierGeneratorProperties() {
return idGenProps;
}

@Override
public void setIdentifierGeneratorStrategy(String s) {
genStrategy = s;
}

@Override
public String getIdentifierGeneratorStrategy() {
return genStrategy;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.hibernate.tool.internal.reveng.util;

import java.util.Properties;

import org.hibernate.mapping.KeyValue;

public interface EnhancedValue extends KeyValue {

void setIdentifierGeneratorProperties(Properties suggestedProperties);

Properties getIdentifierGeneratorProperties();

void setIdentifierGeneratorStrategy(String s);

String getIdentifierGeneratorStrategy();

}

0 comments on commit 2895ada

Please sign in to comment.