Skip to content

Commit ea60c83

Browse files
committed
HBX-3207: Improve the implementation of the 'binder' helper classes
Signed-off-by: Koen Aers <[email protected]>
1 parent 942f9df commit ea60c83

File tree

12 files changed

+1117
-1114
lines changed

12 files changed

+1117
-1114
lines changed

orm/src/main/java/org/hibernate/tool/internal/reveng/binder/BinderUtils.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@
3737
import org.hibernate.tool.api.reveng.RevengStrategy;
3838

3939
public class BinderUtils {
40-
41-
public static Logger LOGGER = Logger.getLogger(BinderUtils.class.getName());
40+
41+
public static Logger LOGGER = Logger.getLogger(BinderUtils.class.getName());
4242

4343
public static String makeUnique(
44-
Iterator<Property> props,
45-
String originalPropertyName) {
44+
Iterator<Property> props,
45+
String originalPropertyName) {
4646
int cnt = 0;
4747
String propertyName = originalPropertyName;
48-
Set<String> uniqueNames = new HashSet<String>();
48+
Set<String> uniqueNames = new HashSet<>();
4949
while ( props.hasNext() ) {
5050
Property element = props.next();
5151
uniqueNames.add( element.getName() );
@@ -58,51 +58,52 @@ public static String makeUnique(
5858
}
5959

6060
public static String makeUnique(PersistentClass clazz, String propertyName) {
61-
List<Property> list = new ArrayList<Property>();
61+
List<Property> list = new ArrayList<>();
6262
if( clazz.hasIdentifierProperty() ) {
6363
list.add( clazz.getIdentifierProperty() );
6464
}
6565
if( clazz.isVersioned() ) {
6666
list.add( clazz.getVersion() );
6767
}
68-
JoinedList<Property> joinedList =
69-
new JoinedList<Property>(
70-
list,
71-
clazz.getProperties());
68+
JoinedList<Property> joinedList =
69+
new JoinedList<>(
70+
list,
71+
clazz.getProperties() );
7272
return BinderUtils.makeUnique(joinedList.iterator(), propertyName);
7373
}
74-
75-
public static String makeUnique(Component clazz, String propertyName) {
74+
75+
public static String makeUnique(Component clazz, String propertyName) {
7676
return BinderUtils.makeUnique(clazz.getProperties().iterator(), propertyName);
7777
}
78-
79-
public static void checkColumnForMultipleBinding(Column column) {
80-
if(column.getValue()!=null) {
81-
LOGGER.log(Level.WARNING, "Binding column twice should not happen. " + column);
78+
79+
public static void checkColumnForMultipleBinding(Column column) {
80+
if(column.getValue()!=null) {
81+
LOGGER.log(Level.WARNING, "Binding column twice should not happen. " + column);
8282
// TODO enable this next line and investigate why the tests fail
8383
// throw new RuntimeException("Binding column twice should not happen. " + column);
84-
}
85-
}
86-
84+
}
85+
}
86+
8787
static void updateFetchMode(Fetchable value, String fetchMode) {
8888
if(FetchMode.JOIN.toString().equalsIgnoreCase(fetchMode)) {
89-
value.setFetchMode(FetchMode.JOIN);
89+
value.setFetchMode(FetchMode.JOIN);
9090
}
9191
else {
92-
value.setFetchMode(FetchMode.SELECT);
93-
}
92+
value.setFetchMode(FetchMode.SELECT);
93+
}
9494
}
9595

9696

9797
static AssociationInfo getAssociationInfo(
98-
RevengStrategy revengStrategy,
99-
ForeignKey foreignKey,
100-
boolean inverseProperty) {
101-
if (inverseProperty) {
102-
return revengStrategy.foreignKeyToInverseAssociationInfo(foreignKey);
103-
} else {
104-
return revengStrategy.foreignKeyToAssociationInfo(foreignKey);
105-
}
98+
RevengStrategy revengStrategy,
99+
ForeignKey foreignKey,
100+
boolean inverseProperty) {
101+
if (inverseProperty) {
102+
return revengStrategy.foreignKeyToInverseAssociationInfo(foreignKey);
103+
}
104+
else {
105+
return revengStrategy.foreignKeyToAssociationInfo(foreignKey);
106+
}
106107
}
107-
108+
108109
}

orm/src/main/java/org/hibernate/tool/internal/reveng/binder/CollectionBinderSecondPass.java

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.hibernate.tool.internal.reveng.binder;
1919

2020
import java.io.ObjectStreamClass;
21+
import java.io.Serial;
2122
import java.lang.reflect.Field;
2223
import java.util.Map;
2324

@@ -30,47 +31,47 @@
3031
import org.hibernate.mapping.Value;
3132

3233
public class CollectionBinderSecondPass extends org.hibernate.boot.model.internal.CollectionSecondPass {
33-
34-
private static final long serialVersionUID =
35-
ObjectStreamClass.lookup(CollectionBinderSecondPass.class).getSerialVersionUID();
3634

37-
private MetadataBuildingContext mdbc;
35+
@Serial
36+
private static final long serialVersionUID =
37+
ObjectStreamClass.lookup( CollectionBinderSecondPass.class).getSerialVersionUID();
38+
39+
private final MetadataBuildingContext mdbc;
3840

3941
public CollectionBinderSecondPass(MetadataBuildingContext mdbc, Collection coll) {
4042
super(coll);
4143
this.mdbc = mdbc;
4244
}
4345

44-
@SuppressWarnings("rawtypes")
45-
public void secondPass(Map persistentClasses) throws MappingException {
46+
public void secondPass(Map persistentClasses) throws MappingException {
4647
bindCollectionSecondPass(getCollection(), mdbc);
47-
}
48+
}
4849

49-
public void doSecondPass(Map<String, PersistentClass> persistentClasses) throws MappingException {
50-
Value element = getCollection().getElement();
51-
DependantValue elementDependantValue = null;
52-
String oldElementForeignKeyName = null;
53-
if(element instanceof DependantValue) {
54-
elementDependantValue = (DependantValue)element;
55-
oldElementForeignKeyName = elementDependantValue.getForeignKeyName();
56-
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!
57-
}
58-
Value key = getCollection().getKey();
59-
DependantValue keyDependantValue = null;
60-
String oldKeyForeignKeyName = null;
61-
if (key instanceof DependantValue) {
62-
keyDependantValue = (DependantValue)key;
63-
oldKeyForeignKeyName = keyDependantValue.getForeignKeyName();
64-
keyDependantValue.setForeignKeyName("none");
65-
}
66-
secondPass(persistentClasses);
50+
public void doSecondPass(Map<String, PersistentClass> persistentClasses) throws MappingException {
51+
Value element = getCollection().getElement();
52+
DependantValue elementDependantValue = null;
53+
String oldElementForeignKeyName = null;
54+
if(element instanceof DependantValue) {
55+
elementDependantValue = (DependantValue)element;
56+
oldElementForeignKeyName = elementDependantValue.getForeignKeyName();
57+
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!
58+
}
59+
Value key = getCollection().getKey();
60+
DependantValue keyDependantValue = null;
61+
String oldKeyForeignKeyName = null;
62+
if (key instanceof DependantValue) {
63+
keyDependantValue = (DependantValue)key;
64+
oldKeyForeignKeyName = keyDependantValue.getForeignKeyName();
65+
keyDependantValue.setForeignKeyName("none");
66+
}
67+
secondPass(persistentClasses);
6768
// super.doSecondPass(persistentClasses);
68-
if(elementDependantValue!=null) {
69-
elementDependantValue.setForeignKeyName(oldElementForeignKeyName);
70-
}
71-
if (keyDependantValue != null) {
72-
keyDependantValue.setForeignKeyName(oldKeyForeignKeyName);
73-
}
69+
if(elementDependantValue!=null) {
70+
elementDependantValue.setForeignKeyName(oldElementForeignKeyName);
71+
}
72+
if (keyDependantValue != null) {
73+
keyDependantValue.setForeignKeyName(oldKeyForeignKeyName);
74+
}
7475
}
7576

7677
private void bindCollectionSecondPass(
@@ -82,25 +83,26 @@ private void bindCollectionSecondPass(
8283

8384
if (persistentClass==null) throw new MappingException(
8485
"Association " + collection.getRole() + " references unmapped class: " + oneToMany.getReferencedEntityName()
85-
);
86+
);
8687

8788
oneToMany.setAssociatedClass(persistentClass); // Child
8889
}
8990
}
90-
91+
9192
private Collection getCollection() {
92-
try {
93-
Field field = getClass().getSuperclass().getDeclaredField("collection");
94-
field.setAccessible(true);
95-
return (Collection)field.get(this);
96-
} catch (NoSuchFieldException e) {
97-
// this will happen if the implementation of the superclass changes
98-
throw new RuntimeException(e);
99-
} catch (IllegalAccessException e) {
100-
// this should not happen
101-
throw new RuntimeException(e);
102-
}
93+
try {
94+
Field field = getClass().getSuperclass().getDeclaredField("collection");
95+
field.setAccessible(true);
96+
return (Collection)field.get(this);
97+
}
98+
catch (NoSuchFieldException e) {
99+
// this will happen if the implementation of the superclass changes
100+
throw new RuntimeException(e);
101+
}
102+
catch (IllegalAccessException e) {
103+
// this should not happen
104+
throw new RuntimeException(e);
105+
}
103106
}
104-
107+
105108
}
106-

orm/src/main/java/org/hibernate/tool/internal/reveng/binder/CollectionPropertyBinder.java

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,43 @@
2525
import org.hibernate.tool.internal.reveng.util.RevengUtils;
2626

2727
class CollectionPropertyBinder extends AbstractBinder {
28-
29-
static CollectionPropertyBinder create(BinderContext binderContext) {
30-
return new CollectionPropertyBinder(binderContext);
31-
}
32-
33-
private final PropertyBinder propertyBinder;
34-
35-
private CollectionPropertyBinder(BinderContext binderContext) {
36-
super(binderContext);
37-
this.propertyBinder = PropertyBinder.create(binderContext);
38-
}
28+
29+
static CollectionPropertyBinder create(BinderContext binderContext) {
30+
return new CollectionPropertyBinder(binderContext);
31+
}
32+
33+
private final PropertyBinder propertyBinder;
34+
35+
private CollectionPropertyBinder(BinderContext binderContext) {
36+
super(binderContext);
37+
this.propertyBinder = PropertyBinder.create(binderContext);
38+
}
3939

4040
Property bind(
41-
String propertyName,
42-
boolean mutable,
43-
Table table,
44-
ForeignKey fk,
45-
Collection value,
46-
boolean inverseProperty) {
47-
AssociationInfo associationInfo = determineAssociationInfo(fk, inverseProperty, mutable);
48-
BinderUtils.updateFetchMode(value, associationInfo.getFetch());
41+
String propertyName,
42+
Table table,
43+
ForeignKey fk,
44+
Collection value) {
45+
AssociationInfo associationInfo = determineAssociationInfo(fk);
46+
BinderUtils.updateFetchMode(value, associationInfo.getFetch());
4947
return propertyBinder.bind(table, propertyName, value, associationInfo);
50-
}
51-
48+
}
49+
5250
private AssociationInfo determineAssociationInfo(
53-
ForeignKey foreignKey,
54-
boolean inverseProperty,
55-
boolean mutable) {
56-
AssociationInfo origin = BinderUtils
57-
.getAssociationInfo(getRevengStrategy(), foreignKey, inverseProperty);
58-
if(origin != null){
59-
return RevengUtils.createAssociationInfo(
60-
origin.getCascade() != null ? origin.getCascade() : "all",
61-
origin.getFetch(),
62-
origin.getInsert() != null ? origin.getInsert() : mutable,
63-
origin.getUpdate() != null ? origin.getUpdate() : mutable
64-
);
65-
} else {
66-
return RevengUtils.createAssociationInfo(null, null, mutable, mutable);
51+
ForeignKey foreignKey) {
52+
AssociationInfo origin = BinderUtils
53+
.getAssociationInfo(getRevengStrategy(), foreignKey, true);
54+
if(origin != null){
55+
return RevengUtils.createAssociationInfo(
56+
origin.getCascade() != null ? origin.getCascade() : "all",
57+
origin.getFetch(),
58+
origin.getInsert() != null ? origin.getInsert() : true,
59+
origin.getUpdate() != null ? origin.getUpdate() : true
60+
);
61+
}
62+
else {
63+
return RevengUtils.createAssociationInfo(null, null, true, true);
6764
}
6865
}
69-
66+
7067
}

orm/src/main/java/org/hibernate/tool/internal/reveng/binder/EntityPropertyBinder.java

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,47 @@
2525
import org.hibernate.tool.internal.reveng.util.RevengUtils;
2626

2727
class EntityPropertyBinder extends AbstractBinder {
28-
29-
static EntityPropertyBinder create(BinderContext binderContext) {
30-
return new EntityPropertyBinder(binderContext);
31-
}
32-
33-
private final PropertyBinder propertyBinder;
34-
35-
private EntityPropertyBinder(BinderContext binderContext) {
36-
super(binderContext);
37-
this.propertyBinder = PropertyBinder.create(binderContext);
38-
}
3928

40-
Property bind(
41-
String propertyName,
42-
boolean mutable,
43-
Table table,
44-
ForeignKey fk,
45-
ToOne value,
46-
boolean inverseProperty) {
47-
AssociationInfo associationInfo = determineAssociationInfo(fk, inverseProperty, mutable);
48-
BinderUtils.updateFetchMode(value, associationInfo.getFetch());
29+
static EntityPropertyBinder create(BinderContext binderContext) {
30+
return new EntityPropertyBinder(binderContext);
31+
}
32+
33+
private final PropertyBinder propertyBinder;
34+
35+
private EntityPropertyBinder(BinderContext binderContext) {
36+
super(binderContext);
37+
this.propertyBinder = PropertyBinder.create(binderContext);
38+
}
39+
40+
Property bind(
41+
String propertyName,
42+
boolean mutable,
43+
Table table,
44+
ForeignKey fk,
45+
ToOne value,
46+
boolean inverseProperty) {
47+
AssociationInfo associationInfo = determineAssociationInfo(fk, inverseProperty, mutable);
48+
BinderUtils.updateFetchMode(value, associationInfo.getFetch());
4949
return propertyBinder.bind(table, propertyName, value, associationInfo);
50-
}
50+
}
5151

5252
private AssociationInfo determineAssociationInfo(
53-
ForeignKey foreignKey,
54-
boolean inverseProperty,
55-
boolean mutable) {
56-
AssociationInfo origin = BinderUtils
57-
.getAssociationInfo(getRevengStrategy(), foreignKey, inverseProperty);
58-
if(origin != null){
59-
return RevengUtils.createAssociationInfo(
60-
origin.getCascade(),
61-
origin.getFetch(),
62-
origin.getInsert() != null ? origin.getInsert() : mutable,
63-
origin.getUpdate() != null ? origin.getUpdate() : mutable
64-
);
65-
} else {
66-
return RevengUtils.createAssociationInfo(null, null, mutable, mutable);
53+
ForeignKey foreignKey,
54+
boolean inverseProperty,
55+
boolean mutable) {
56+
AssociationInfo origin = BinderUtils
57+
.getAssociationInfo(getRevengStrategy(), foreignKey, inverseProperty);
58+
if(origin != null){
59+
return RevengUtils.createAssociationInfo(
60+
origin.getCascade(),
61+
origin.getFetch(),
62+
origin.getInsert() != null ? origin.getInsert() : mutable,
63+
origin.getUpdate() != null ? origin.getUpdate() : mutable
64+
);
65+
}
66+
else {
67+
return RevengUtils.createAssociationInfo(null, null, mutable, mutable);
6768
}
6869
}
69-
70+
7071
}

0 commit comments

Comments
 (0)