Skip to content
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 @@ -37,15 +37,15 @@
import org.hibernate.tool.api.reveng.RevengStrategy;

public class BinderUtils {
public static Logger LOGGER = Logger.getLogger(BinderUtils.class.getName());

public static Logger LOGGER = Logger.getLogger(BinderUtils.class.getName());

public static String makeUnique(
Iterator<Property> props,
String originalPropertyName) {
Iterator<Property> props,
String originalPropertyName) {
int cnt = 0;
String propertyName = originalPropertyName;
Set<String> uniqueNames = new HashSet<String>();
Set<String> uniqueNames = new HashSet<>();
while ( props.hasNext() ) {
Property element = props.next();
uniqueNames.add( element.getName() );
Expand All @@ -58,51 +58,52 @@ public static String makeUnique(
}

public static String makeUnique(PersistentClass clazz, String propertyName) {
List<Property> list = new ArrayList<Property>();
List<Property> list = new ArrayList<>();
if( clazz.hasIdentifierProperty() ) {
list.add( clazz.getIdentifierProperty() );
}
if( clazz.isVersioned() ) {
list.add( clazz.getVersion() );
}
JoinedList<Property> joinedList =
new JoinedList<Property>(
list,
clazz.getProperties());
JoinedList<Property> joinedList =
new JoinedList<>(
list,
clazz.getProperties() );
return BinderUtils.makeUnique(joinedList.iterator(), propertyName);
}
public static String makeUnique(Component clazz, String propertyName) {

public static String makeUnique(Component clazz, String propertyName) {
return BinderUtils.makeUnique(clazz.getProperties().iterator(), propertyName);
}
public static void checkColumnForMultipleBinding(Column column) {
if(column.getValue()!=null) {
LOGGER.log(Level.WARNING, "Binding column twice should not happen. " + column);

public static void checkColumnForMultipleBinding(Column column) {
if(column.getValue()!=null) {
LOGGER.log(Level.WARNING, "Binding column twice should not happen. " + column);
// TODO enable this next line and investigate why the tests fail
// throw new RuntimeException("Binding column twice should not happen. " + column);
}
}
}
}

static void updateFetchMode(Fetchable value, String fetchMode) {
if(FetchMode.JOIN.toString().equalsIgnoreCase(fetchMode)) {
value.setFetchMode(FetchMode.JOIN);
value.setFetchMode(FetchMode.JOIN);
}
else {
value.setFetchMode(FetchMode.SELECT);
}
value.setFetchMode(FetchMode.SELECT);
}
}


static AssociationInfo getAssociationInfo(
RevengStrategy revengStrategy,
ForeignKey foreignKey,
boolean inverseProperty) {
if (inverseProperty) {
return revengStrategy.foreignKeyToInverseAssociationInfo(foreignKey);
} else {
return revengStrategy.foreignKeyToAssociationInfo(foreignKey);
}
RevengStrategy revengStrategy,
ForeignKey foreignKey,
boolean inverseProperty) {
if (inverseProperty) {
return revengStrategy.foreignKeyToInverseAssociationInfo(foreignKey);
}
else {
return revengStrategy.foreignKeyToAssociationInfo(foreignKey);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.hibernate.tool.internal.reveng.binder;

import java.io.ObjectStreamClass;
import java.io.Serial;
import java.lang.reflect.Field;
import java.util.Map;

Expand All @@ -30,47 +31,47 @@
import org.hibernate.mapping.Value;

public class CollectionBinderSecondPass extends org.hibernate.boot.model.internal.CollectionSecondPass {

private static final long serialVersionUID =
ObjectStreamClass.lookup(CollectionBinderSecondPass.class).getSerialVersionUID();

private MetadataBuildingContext mdbc;
@Serial
private static final long serialVersionUID =
ObjectStreamClass.lookup( CollectionBinderSecondPass.class).getSerialVersionUID();

private final MetadataBuildingContext mdbc;

public CollectionBinderSecondPass(MetadataBuildingContext mdbc, Collection coll) {
super(coll);
this.mdbc = mdbc;
}

@SuppressWarnings("rawtypes")
public void secondPass(Map persistentClasses) throws MappingException {
public void secondPass(Map persistentClasses) throws MappingException {
bindCollectionSecondPass(getCollection(), mdbc);
}
}

public void doSecondPass(Map<String, PersistentClass> persistentClasses) throws MappingException {
Value element = getCollection().getElement();
DependantValue elementDependantValue = null;
String oldElementForeignKeyName = null;
if(element instanceof DependantValue) {
elementDependantValue = (DependantValue)element;
oldElementForeignKeyName = elementDependantValue.getForeignKeyName();
elementDependantValue.setForeignKeyName("none"); // Workaround to avoid DependantValue to create foreignkey just because reference columns are not the same + no need to create keys already in the db!
}
Value key = getCollection().getKey();
DependantValue keyDependantValue = null;
String oldKeyForeignKeyName = null;
if (key instanceof DependantValue) {
keyDependantValue = (DependantValue)key;
oldKeyForeignKeyName = keyDependantValue.getForeignKeyName();
keyDependantValue.setForeignKeyName("none");
}
secondPass(persistentClasses);
public void doSecondPass(Map<String, PersistentClass> persistentClasses) throws MappingException {
Value element = getCollection().getElement();
DependantValue elementDependantValue = null;
String oldElementForeignKeyName = null;
if(element instanceof DependantValue) {
elementDependantValue = (DependantValue)element;
oldElementForeignKeyName = elementDependantValue.getForeignKeyName();
elementDependantValue.setForeignKeyName("none"); // Workaround to avoid DependantValue to create foreignkey just because reference columns are not the same + no need to create keys already in the db!
}
Value key = getCollection().getKey();
DependantValue keyDependantValue = null;
String oldKeyForeignKeyName = null;
if (key instanceof DependantValue) {
keyDependantValue = (DependantValue)key;
oldKeyForeignKeyName = keyDependantValue.getForeignKeyName();
keyDependantValue.setForeignKeyName("none");
}
secondPass(persistentClasses);
// super.doSecondPass(persistentClasses);
if(elementDependantValue!=null) {
elementDependantValue.setForeignKeyName(oldElementForeignKeyName);
}
if (keyDependantValue != null) {
keyDependantValue.setForeignKeyName(oldKeyForeignKeyName);
}
if(elementDependantValue!=null) {
elementDependantValue.setForeignKeyName(oldElementForeignKeyName);
}
if (keyDependantValue != null) {
keyDependantValue.setForeignKeyName(oldKeyForeignKeyName);
}
}

private void bindCollectionSecondPass(
Expand All @@ -82,25 +83,26 @@ private void bindCollectionSecondPass(

if (persistentClass==null) throw new MappingException(
"Association " + collection.getRole() + " references unmapped class: " + oneToMany.getReferencedEntityName()
);
);

oneToMany.setAssociatedClass(persistentClass); // Child
}
}

private Collection getCollection() {
try {
Field field = getClass().getSuperclass().getDeclaredField("collection");
field.setAccessible(true);
return (Collection)field.get(this);
} catch (NoSuchFieldException e) {
// this will happen if the implementation of the superclass changes
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
// this should not happen
throw new RuntimeException(e);
}
try {
Field field = getClass().getSuperclass().getDeclaredField("collection");
field.setAccessible(true);
return (Collection)field.get(this);
}
catch (NoSuchFieldException e) {
// this will happen if the implementation of the superclass changes
throw new RuntimeException(e);
}
catch (IllegalAccessException e) {
// this should not happen
throw new RuntimeException(e);
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,43 @@
import org.hibernate.tool.internal.reveng.util.RevengUtils;

class CollectionPropertyBinder extends AbstractBinder {
static CollectionPropertyBinder create(BinderContext binderContext) {
return new CollectionPropertyBinder(binderContext);
}
private final PropertyBinder propertyBinder;
private CollectionPropertyBinder(BinderContext binderContext) {
super(binderContext);
this.propertyBinder = PropertyBinder.create(binderContext);
}

static CollectionPropertyBinder create(BinderContext binderContext) {
return new CollectionPropertyBinder(binderContext);
}

private final PropertyBinder propertyBinder;

private CollectionPropertyBinder(BinderContext binderContext) {
super(binderContext);
this.propertyBinder = PropertyBinder.create(binderContext);
}

Property bind(
String propertyName,
boolean mutable,
Table table,
ForeignKey fk,
Collection value,
boolean inverseProperty) {
AssociationInfo associationInfo = determineAssociationInfo(fk, inverseProperty, mutable);
BinderUtils.updateFetchMode(value, associationInfo.getFetch());
String propertyName,
Table table,
ForeignKey fk,
Collection value) {
AssociationInfo associationInfo = determineAssociationInfo(fk);
BinderUtils.updateFetchMode(value, associationInfo.getFetch());
return propertyBinder.bind(table, propertyName, value, associationInfo);
}
}

private AssociationInfo determineAssociationInfo(
ForeignKey foreignKey,
boolean inverseProperty,
boolean mutable) {
AssociationInfo origin = BinderUtils
.getAssociationInfo(getRevengStrategy(), foreignKey, inverseProperty);
if(origin != null){
return RevengUtils.createAssociationInfo(
origin.getCascade() != null ? origin.getCascade() : "all",
origin.getFetch(),
origin.getInsert() != null ? origin.getInsert() : mutable,
origin.getUpdate() != null ? origin.getUpdate() : mutable
);
} else {
return RevengUtils.createAssociationInfo(null, null, mutable, mutable);
ForeignKey foreignKey) {
AssociationInfo origin = BinderUtils
.getAssociationInfo(getRevengStrategy(), foreignKey, true);
if(origin != null){
return RevengUtils.createAssociationInfo(
origin.getCascade() != null ? origin.getCascade() : "all",
origin.getFetch(),
origin.getInsert() != null ? origin.getInsert() : true,
origin.getUpdate() != null ? origin.getUpdate() : true
);
}
else {
return RevengUtils.createAssociationInfo(null, null, true, true);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,47 @@
import org.hibernate.tool.internal.reveng.util.RevengUtils;

class EntityPropertyBinder extends AbstractBinder {

static EntityPropertyBinder create(BinderContext binderContext) {
return new EntityPropertyBinder(binderContext);
}

private final PropertyBinder propertyBinder;

private EntityPropertyBinder(BinderContext binderContext) {
super(binderContext);
this.propertyBinder = PropertyBinder.create(binderContext);
}

Property bind(
String propertyName,
boolean mutable,
Table table,
ForeignKey fk,
ToOne value,
boolean inverseProperty) {
AssociationInfo associationInfo = determineAssociationInfo(fk, inverseProperty, mutable);
BinderUtils.updateFetchMode(value, associationInfo.getFetch());
static EntityPropertyBinder create(BinderContext binderContext) {
return new EntityPropertyBinder(binderContext);
}

private final PropertyBinder propertyBinder;

private EntityPropertyBinder(BinderContext binderContext) {
super(binderContext);
this.propertyBinder = PropertyBinder.create(binderContext);
}

Property bind(
String propertyName,
boolean mutable,
Table table,
ForeignKey fk,
ToOne value,
boolean inverseProperty) {
AssociationInfo associationInfo = determineAssociationInfo(fk, inverseProperty, mutable);
BinderUtils.updateFetchMode(value, associationInfo.getFetch());
return propertyBinder.bind(table, propertyName, value, associationInfo);
}
}

private AssociationInfo determineAssociationInfo(
ForeignKey foreignKey,
boolean inverseProperty,
boolean mutable) {
AssociationInfo origin = BinderUtils
.getAssociationInfo(getRevengStrategy(), foreignKey, inverseProperty);
if(origin != null){
return RevengUtils.createAssociationInfo(
origin.getCascade(),
origin.getFetch(),
origin.getInsert() != null ? origin.getInsert() : mutable,
origin.getUpdate() != null ? origin.getUpdate() : mutable
);
} else {
return RevengUtils.createAssociationInfo(null, null, mutable, mutable);
ForeignKey foreignKey,
boolean inverseProperty,
boolean mutable) {
AssociationInfo origin = BinderUtils
.getAssociationInfo(getRevengStrategy(), foreignKey, inverseProperty);
if(origin != null){
return RevengUtils.createAssociationInfo(
origin.getCascade(),
origin.getFetch(),
origin.getInsert() != null ? origin.getInsert() : mutable,
origin.getUpdate() != null ? origin.getUpdate() : mutable
);
}
else {
return RevengUtils.createAssociationInfo(null, null, mutable, mutable);
}
}

}
Loading