Skip to content

Commit 772f3cb

Browse files
HHH-8171 - Cleanup
1 parent 120237e commit 772f3cb

File tree

6 files changed

+37
-46
lines changed

6 files changed

+37
-46
lines changed

documentation/src/main/docbook/devguide/en-US/Envers.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@
323323
SETORDINAL
324324
</entry>
325325
<entry>
326-
The name of the column used for storing the ordinal of the change in sets of embeddables.
326+
Name of column used for storing ordinal of the change in sets of embeddable elements.
327327
</entry>
328328
</row>
329329
</tbody>

hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditEntitiesConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ public AuditEntitiesConfiguration(Properties properties, String revisionInfoEnti
115115
revisionNumberPath = originalIdPropName + "." + revisionFieldName + ".id";
116116
revisionPropBasePath = originalIdPropName + "." + revisionFieldName + ".";
117117

118-
embeddableSetOrdinalPropertyName = getProperty( properties,
119-
"org.hibernate.envers.embeddable_set_ordinal_field_name",
120-
"org.hibernate.envers.embeddable_set_ordinal_field_name", "SETORDINAL" );
118+
embeddableSetOrdinalPropertyName = getProperty(
119+
properties, "org.hibernate.envers.embeddable_set_ordinal_field_name",
120+
"org.hibernate.envers.embeddable_set_ordinal_field_name", "SETORDINAL"
121+
);
121122
}
122123

123124
public String getOriginalIdPropName() {

hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/CollectionMetadataGenerator.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
import org.hibernate.mapping.OneToMany;
8888
import org.hibernate.mapping.PersistentClass;
8989
import org.hibernate.mapping.Property;
90-
import org.hibernate.mapping.SimpleValue;
9190
import org.hibernate.mapping.Table;
9291
import org.hibernate.mapping.Value;
9392
import org.hibernate.type.BagType;
@@ -506,20 +505,16 @@ private MiddleComponentData addValueToMiddleTable(Value value, Element xmlMappin
506505
);
507506
}
508507

509-
// Add an additional column holding a number to make each entry unique within the set,
510-
// since embeddable properties can be null
511-
if ( propertyValue.getCollectionType() instanceof SetType ) {
512-
final String auditedEmbeddableSetOrdinalPropertyName = mainGenerator.getVerEntCfg()
513-
.getEmbeddableSetOrdinalPropertyName();
514-
final String auditedEmbeddableSetOrdinalPropertyType = "integer";
515-
516-
final SimpleValue simpleValue = new SimpleValue( component.getMappings(), component.getTable() );
517-
simpleValue.setTypeName( auditedEmbeddableSetOrdinalPropertyType );
518-
519-
final Element idProperty = MetadataTools.addProperty( xmlMapping,
520-
auditedEmbeddableSetOrdinalPropertyName, auditedEmbeddableSetOrdinalPropertyType, true, true );
521-
MetadataTools.addColumn( idProperty, auditedEmbeddableSetOrdinalPropertyName, null, 0, 0, null, null,
522-
null, false );
508+
// Add an additional column holding a number to make each entry unique within the set.
509+
// Embeddable properties may contain null values, so cannot be stored within composite primary key.
510+
if ( propertyValue.isSet() ) {
511+
final String setOrdinalPropertyName = mainGenerator.getVerEntCfg().getEmbeddableSetOrdinalPropertyName();
512+
final Element ordinalProperty = MetadataTools.addProperty(
513+
xmlMapping, setOrdinalPropertyName, "integer", true, true
514+
);
515+
MetadataTools.addColumn(
516+
ordinalProperty, setOrdinalPropertyName, null, null, null, null, null, null, false
517+
);
523518
}
524519

525520
return new MiddleComponentData( componentMapper, 0 );

hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/AbstractCollectionMapper.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,16 @@ protected AbstractCollectionMapper(CommonCollectionMapperData commonCollectionMa
8787
protected abstract void mapToMapFromObject(SessionImplementor session, Map<String, Object> idData, Map<String, Object> data, Object changed);
8888

8989
/**
90-
* Creates a Map for the id.
91-
*
92-
* <p>
93-
* The ordinal parameter represents the iteration ordinal of the current element, used to add a synthetic id when
94-
* dealing with embeddables since embeddable fields can't be contained within the primary key since they might be
95-
* nullable.
96-
* </p>
97-
*
98-
* @param ordinal
99-
* The element iteration ordinal.
100-
*
101-
* @return A Map for holding the ID information.
90+
* Creates map for storing identifier data. Ordinal parameter guarantees uniqueness of primary key.
91+
* Composite primary key cannot contain embeddable properties since they might be nullable.
92+
* @param ordinal Iteration ordinal.
93+
* @return Map for holding identifier data.
10294
*/
10395
protected Map<String, Object> createIdMap(int ordinal) {
104-
final HashMap<String, Object> idMap = new HashMap<String, Object>();
105-
96+
final Map<String, Object> idMap = new HashMap<String, Object>();
10697
if ( ordinalInId ) {
107-
idMap.put( this.commonCollectionMapperData.getVerEntCfg().getEmbeddableSetOrdinalPropertyName(),
108-
Integer.valueOf( ordinal ) );
98+
idMap.put( commonCollectionMapperData.getVerEntCfg().getEmbeddableSetOrdinalPropertyName(), ordinal );
10999
}
110-
111100
return idMap;
112101
}
113102

hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/SortedSetCollectionMapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public SortedSetCollectionMapper(CommonCollectionMapperData commonCollectionMapp
4141
Class<? extends SortedSet> collectionClass, Class<? extends SortedSet> proxyClass,
4242
MiddleComponentData elementComponentData, Comparator comparator, boolean ordinalInId,
4343
boolean revisionTypeInId) {
44-
super( commonCollectionMapperData, collectionClass, proxyClass, elementComponentData, ordinalInId,
45-
revisionTypeInId );
44+
super( commonCollectionMapperData, collectionClass, proxyClass, elementComponentData, ordinalInId, revisionTypeInId );
4645
this.comparator = comparator;
4746
}
4847

hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/embeddable/EmbeddableSet.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package org.hibernate.envers.test.integration.collection.embeddable;
2525

2626
import java.util.Arrays;
27-
import java.util.Collections;
2827
import javax.persistence.EntityManager;
2928

3029
import org.junit.Test;
@@ -65,7 +64,7 @@ public void initData() {
6564

6665
EmbeddableSetEntity ese1 = new EmbeddableSetEntity();
6766

68-
// Revision 1 (ese1: initially 2 elements)
67+
// Revision 1 (ese1: initially two elements)
6968
em.getTransaction().begin();
7069
ese1.getComponentSet().add( c3_1 );
7170
ese1.getComponentSet().add( c3_3 );
@@ -84,33 +83,33 @@ public void initData() {
8483
ese1.getComponentSet().add( c3_2 );
8584
em.getTransaction().commit();
8685

87-
// Revision 3 (ese1: adding one existing element)
86+
// Revision (still 2) (ese1: adding one existing element)
8887
em.getTransaction().begin();
8988
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
9089
ese1.getComponentSet().add( c3_1 );
9190
em.getTransaction().commit();
9291

93-
// Revision 4 (ese1: removing one existing element)
92+
// Revision 3 (ese1: removing one existing element)
9493
em.getTransaction().begin();
9594
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
9695
ese1.getComponentSet().remove( c3_2 );
9796
em.getTransaction().commit();
9897

99-
// Revision 5 (ese1: adding two elements)
98+
// Revision 4 (ese1: adding two elements)
10099
em.getTransaction().begin();
101100
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
102101
ese1.getComponentSet().add( c3_2 );
103102
ese1.getComponentSet().add( c3_4 );
104103
em.getTransaction().commit();
105104

106-
// Revision 6 (ese1: removing two elements)
105+
// Revision 5 (ese1: removing two elements)
107106
em.getTransaction().begin();
108107
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
109108
ese1.getComponentSet().remove( c3_2 );
110109
ese1.getComponentSet().remove( c3_4 );
111110
em.getTransaction().commit();
112111

113-
// Revision 7 (ese1: removing and adding two elements)
112+
// Revision 6 (ese1: removing and adding two elements)
114113
em.getTransaction().begin();
115114
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
116115
ese1.getComponentSet().remove( c3_1 );
@@ -119,14 +118,20 @@ public void initData() {
119118
ese1.getComponentSet().add( c3_4 );
120119
em.getTransaction().commit();
121120

121+
// Revision 7 (ese1: adding one element)
122+
em.getTransaction().begin();
123+
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
124+
ese1.getComponentSet().add( c3_1 );
125+
em.getTransaction().commit();
126+
122127
ese1_id = ese1.getId();
123128

124129
em.close();
125130
}
126131

127132
@Test
128133
public void testRevisionsCounts() {
129-
assertEquals( Arrays.asList( 1, 2, 3, 4, 5, 6 ), getAuditReader().getRevisions( EmbeddableSetEntity.class, ese1_id ) );
134+
assertEquals( Arrays.asList( 1, 2, 3, 4, 5, 6, 7 ), getAuditReader().getRevisions( EmbeddableSetEntity.class, ese1_id ) );
130135
}
131136

132137
@Test
@@ -137,12 +142,14 @@ public void testHistoryOfEse1() {
137142
EmbeddableSetEntity rev4 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 4 );
138143
EmbeddableSetEntity rev5 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 5 );
139144
EmbeddableSetEntity rev6 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 6 );
145+
EmbeddableSetEntity rev7 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 7 );
140146

141147
assertEquals( TestTools.makeSet( c3_1, c3_3 ), rev1.getComponentSet() );
142148
assertEquals( TestTools.makeSet( c3_1, c3_2, c3_3 ), rev2.getComponentSet() );
143149
assertEquals( TestTools.makeSet( c3_1, c3_3 ), rev3.getComponentSet() );
144150
assertEquals( TestTools.makeSet( c3_1, c3_2, c3_3, c3_4 ), rev4.getComponentSet() );
145151
assertEquals( TestTools.makeSet( c3_1, c3_3 ), rev5.getComponentSet() );
146152
assertEquals( TestTools.makeSet( c3_2, c3_4 ), rev6.getComponentSet() );
153+
assertEquals( TestTools.makeSet( c3_2, c3_4, c3_1 ), rev7.getComponentSet() );
147154
}
148155
}

0 commit comments

Comments
 (0)