Skip to content

Commit

Permalink
HHH-8171 - SETORDINAL to support set of embeddables
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofferlundberg authored and lukasz-antoniak committed Apr 14, 2013
1 parent 8bca70e commit 4796553
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 30 deletions.
11 changes: 11 additions & 0 deletions documentation/src/main/docbook/devguide/en-US/Envers.xml
Expand Up @@ -315,6 +315,17 @@
For example: a property called "age", will by default get modified flag with column name "age_MOD".
</entry>
</row>
<row>
<entry>
<property>org.hibernate.envers.embeddable_set_ordinal_field_name</property>
</entry>
<entry>
SETORDINAL
</entry>
<entry>
The name of the column used for storing the ordinal of the change in sets of embeddables.
</entry>
</row>
</tbody>
</tgroup>
</table>
Expand Down
Expand Up @@ -99,4 +99,9 @@ public interface EnversSettings {
* Defaults to {@literal REVEND_TSTMP}.
*/
public static final String AUDIT_STRATEGY_VALIDITY_REVEND_TIMESTAMP_FIELD_NAME = "org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name";

/**
* The name of the column used for storing the ordinal of the change in sets of embeddables. Defaults to {@literal SETORDINAL}.
*/
public static final String EMBEDDABLE_SET_ORDINAL_FIELD_NAME = "org.hibernate.envers.embeddable_set_ordinal_field_name";
}
Expand Up @@ -59,6 +59,8 @@ public class AuditEntitiesConfiguration {

private final boolean revisionEndTimestampEnabled;
private final String revisionEndTimestampFieldName;

private final String embeddableSetOrdinalPropertyName;

public AuditEntitiesConfiguration(Properties properties, String revisionInfoEntityName) {
this.revisionInfoEntityName = revisionInfoEntityName;
Expand Down Expand Up @@ -100,6 +102,9 @@ public AuditEntitiesConfiguration(Properties properties, String revisionInfoEnti

revisionNumberPath = originalIdPropName + "." + revisionFieldName + ".id";
revisionPropBasePath = originalIdPropName + "." + revisionFieldName + ".";

embeddableSetOrdinalPropertyName = ConfigurationHelper.getString(
EnversSettings.EMBEDDABLE_SET_ORDINAL_FIELD_NAME, properties, "SETORDINAL" );
}

public String getOriginalIdPropName() {
Expand Down Expand Up @@ -167,4 +172,8 @@ public String getAuditStrategyName() {
public String getRevisionEndFieldName() {
return revisionEndFieldName;
}

public String getEmbeddableSetOrdinalPropertyName() {
return embeddableSetOrdinalPropertyName;
}
}
Expand Up @@ -88,6 +88,7 @@
import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.Value;
import org.hibernate.type.BagType;
Expand Down Expand Up @@ -506,6 +507,22 @@ private MiddleComponentData addValueToMiddleTable(Value value, Element xmlMappin
);
}

// Add an additional column holding a number to make each entry unique within the set,
// since embeddable properties can be null
if ( propertyValue.getCollectionType() instanceof SetType ) {
final String auditedEmbeddableSetOrdinalPropertyName = mainGenerator.getVerEntCfg()
.getEmbeddableSetOrdinalPropertyName();
final String auditedEmbeddableSetOrdinalPropertyType = "integer";

final SimpleValue simpleValue = new SimpleValue( component.getMappings(), component.getTable() );
simpleValue.setTypeName( auditedEmbeddableSetOrdinalPropertyType );

final Element idProperty = MetadataTools.addProperty( xmlMapping,
auditedEmbeddableSetOrdinalPropertyName, auditedEmbeddableSetOrdinalPropertyType, true, true );
MetadataTools.addColumn( idProperty, auditedEmbeddableSetOrdinalPropertyName, null, 0, 0, null, null,
null, false );
}

return new MiddleComponentData( componentMapper, 0 );
} else {
// Last but one parameter: collection components are always insertable
Expand All @@ -529,14 +546,13 @@ private void addMapper(CommonCollectionMapperData commonCollectionMapperData, Mi
Type type = propertyValue.getType();
boolean embeddableElementType = isEmbeddableElementType();
if (type instanceof SortedSetType) {
currentMapper.addComposite(propertyAuditingData.getPropertyData(),
new SortedSetCollectionMapper(commonCollectionMapperData,
TreeSet.class, SortedSetProxy.class, elementComponentData, propertyValue.getComparator(),
embeddableElementType));
currentMapper.addComposite( propertyAuditingData.getPropertyData(), new SortedSetCollectionMapper(
commonCollectionMapperData, TreeSet.class, SortedSetProxy.class, elementComponentData,
propertyValue.getComparator(), embeddableElementType, embeddableElementType ) );
} else if (type instanceof SetType) {
currentMapper.addComposite(propertyAuditingData.getPropertyData(),
new BasicCollectionMapper<Set>(commonCollectionMapperData,
HashSet.class, SetProxy.class, elementComponentData, embeddableElementType));
currentMapper.addComposite( propertyAuditingData.getPropertyData(), new BasicCollectionMapper<Set>(
commonCollectionMapperData, HashSet.class, SetProxy.class, elementComponentData,
embeddableElementType, embeddableElementType ) );
} else if (type instanceof SortedMapType) {
// Indexed collection, so <code>indexComponentData</code> is not null.
currentMapper.addComposite(propertyAuditingData.getPropertyData(),
Expand All @@ -549,9 +565,9 @@ private void addMapper(CommonCollectionMapperData commonCollectionMapperData, Mi
new MapCollectionMapper<Map>(commonCollectionMapperData,
HashMap.class, MapProxy.class, elementComponentData, indexComponentData, embeddableElementType));
} else if (type instanceof BagType) {
currentMapper.addComposite(propertyAuditingData.getPropertyData(),
new BasicCollectionMapper<List>(commonCollectionMapperData,
ArrayList.class, ListProxy.class, elementComponentData, embeddableElementType));
currentMapper.addComposite( propertyAuditingData.getPropertyData(), new BasicCollectionMapper<List>(
commonCollectionMapperData, ArrayList.class, ListProxy.class, elementComponentData,
embeddableElementType, embeddableElementType ) );
} else if (type instanceof ListType) {
// Indexed collection, so <code>indexComponentData</code> is not null.
currentMapper.addComposite(propertyAuditingData.getPropertyData(),
Expand Down
Expand Up @@ -55,15 +55,17 @@
public abstract class AbstractCollectionMapper<T> implements PropertyMapper {
protected final CommonCollectionMapperData commonCollectionMapperData;
protected final Class<? extends T> collectionClass;
protected final boolean ordinalInId;
protected final boolean revisionTypeInId;

private final Constructor<? extends T> proxyConstructor;

protected AbstractCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
Class<? extends T> collectionClass, Class<? extends T> proxyClass,
boolean revisionTypeInId) {
protected AbstractCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
Class<? extends T> collectionClass, Class<? extends T> proxyClass, boolean ordinalInId,
boolean revisionTypeInId) {
this.commonCollectionMapperData = commonCollectionMapperData;
this.collectionClass = collectionClass;
this.ordinalInId = ordinalInId;
this.revisionTypeInId = revisionTypeInId;

try {
Expand All @@ -84,11 +86,38 @@ protected AbstractCollectionMapper(CommonCollectionMapperData commonCollectionMa
*/
protected abstract void mapToMapFromObject(SessionImplementor session, Map<String, Object> idData, Map<String, Object> data, Object changed);

/**
* Creates a Map for the id.
*
* <p>
* The ordinal parameter represents the iteration ordinal of the current element, used to add a synthetic id when
* dealing with embeddables since embeddable fields can't be contained within the primary key since they might be
* nullable.
* </p>
*
* @param ordinal
* The element iteration ordinal.
*
* @return A Map for holding the ID information.
*/
protected Map<String, Object> createIdMap(int ordinal) {
final HashMap<String, Object> idMap = new HashMap<String, Object>();

if ( ordinalInId ) {
idMap.put( this.commonCollectionMapperData.getVerEntCfg().getEmbeddableSetOrdinalPropertyName(),
Integer.valueOf( ordinal ) );
}

return idMap;
}

private void addCollectionChanges(SessionImplementor session, List<PersistentCollectionChangeData> collectionChanges,
Set<Object> changed, RevisionType revisionType, Serializable id) {
int ordinal = 0;

for (Object changedObj : changed) {
Map<String, Object> entityData = new HashMap<String, Object>();
Map<String, Object> originalId = new HashMap<String, Object>();
Map<String, Object> originalId = createIdMap( ordinal++ );
entityData.put(commonCollectionMapperData.getVerEntCfg().getOriginalIdPropName(), originalId);

collectionChanges.add(new PersistentCollectionChangeData(
Expand Down
Expand Up @@ -41,10 +41,10 @@
public class BasicCollectionMapper<T extends Collection> extends AbstractCollectionMapper<T> implements PropertyMapper {
protected final MiddleComponentData elementComponentData;

public BasicCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
Class<? extends T> collectionClass, Class<? extends T> proxyClass,
MiddleComponentData elementComponentData, boolean revisionTypeInId) {
super(commonCollectionMapperData, collectionClass, proxyClass, revisionTypeInId);
public BasicCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
Class<? extends T> collectionClass, Class<? extends T> proxyClass,
MiddleComponentData elementComponentData, boolean ordinalInId, boolean revisionTypeInId) {
super( commonCollectionMapperData, collectionClass, proxyClass, ordinalInId, revisionTypeInId );
this.elementComponentData = elementComponentData;
}

Expand Down
Expand Up @@ -49,7 +49,7 @@ public final class ListCollectionMapper extends AbstractCollectionMapper<List> i
public ListCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
MiddleComponentData elementComponentData, MiddleComponentData indexComponentData,
boolean revisionTypeInId) {
super(commonCollectionMapperData, List.class, ListProxy.class, revisionTypeInId);
super( commonCollectionMapperData, List.class, ListProxy.class, false, revisionTypeInId );
this.elementComponentData = elementComponentData;
this.indexComponentData = indexComponentData;
}
Expand Down
Expand Up @@ -46,7 +46,7 @@ public MapCollectionMapper(CommonCollectionMapperData commonCollectionMapperData
Class<? extends T> collectionClass, Class<? extends T> proxyClass,
MiddleComponentData elementComponentData, MiddleComponentData indexComponentData,
boolean revisionTypeInId) {
super(commonCollectionMapperData, collectionClass, proxyClass, revisionTypeInId);
super( commonCollectionMapperData, collectionClass, proxyClass, false, revisionTypeInId );
this.elementComponentData = elementComponentData;
this.indexComponentData = indexComponentData;
}
Expand Down
Expand Up @@ -38,10 +38,11 @@ public final class SortedSetCollectionMapper extends BasicCollectionMapper<Sorte
private final Comparator comparator;

public SortedSetCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
Class<? extends SortedSet> collectionClass, Class<? extends SortedSet> proxyClass,
MiddleComponentData elementComponentData, Comparator comparator,
boolean revisionTypeInId) {
super(commonCollectionMapperData, collectionClass, proxyClass, elementComponentData, revisionTypeInId);
Class<? extends SortedSet> collectionClass, Class<? extends SortedSet> proxyClass,
MiddleComponentData elementComponentData, Comparator comparator, boolean ordinalInId,
boolean revisionTypeInId) {
super( commonCollectionMapperData, collectionClass, proxyClass, elementComponentData, ordinalInId,
revisionTypeInId );
this.comparator = comparator;
}

Expand Down
Expand Up @@ -50,6 +50,8 @@ public class EmbeddableSet extends BaseEnversJPAFunctionalTestCase {
private final Component4 c4_2 = new Component4( "c42", "c42_value2", "c42_description" );
private final Component3 c3_1 = new Component3( "c31", c4_1, c4_2 );
private final Component3 c3_2 = new Component3( "c32", c4_1, c4_2 );
private final Component3 c3_3 = new Component3( "c33", c4_1, c4_2 );
private final Component3 c3_4 = new Component3( "c34", c4_1, c4_2 );

@Override
protected Class<?>[] getAnnotatedClasses() {
Expand All @@ -63,9 +65,10 @@ public void initData() {

EmbeddableSetEntity ese1 = new EmbeddableSetEntity();

// Revision 1 (ese1: initially 1 element in both collections)
// Revision 1 (ese1: initially 2 elements)
em.getTransaction().begin();
ese1.getComponentSet().add( c3_1 );
ese1.getComponentSet().add( c3_3 );
em.persist( ese1 );
em.getTransaction().commit();

Expand Down Expand Up @@ -93,24 +96,53 @@ public void initData() {
ese1.getComponentSet().remove( c3_2 );
em.getTransaction().commit();

// Revision 5 (ese1: adding two elements)
em.getTransaction().begin();
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
ese1.getComponentSet().add( c3_2 );
ese1.getComponentSet().add( c3_4 );
em.getTransaction().commit();

// Revision 6 (ese1: removing two elements)
em.getTransaction().begin();
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
ese1.getComponentSet().remove( c3_2 );
ese1.getComponentSet().remove( c3_4 );
em.getTransaction().commit();

// Revision 7 (ese1: removing and adding two elements)
em.getTransaction().begin();
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
ese1.getComponentSet().remove( c3_1 );
ese1.getComponentSet().remove( c3_3 );
ese1.getComponentSet().add( c3_2 );
ese1.getComponentSet().add( c3_4 );
em.getTransaction().commit();

ese1_id = ese1.getId();

em.close();
}

@Test
public void testRevisionsCounts() {
assertEquals( Arrays.asList( 1, 2, 3 ), getAuditReader().getRevisions( EmbeddableSetEntity.class, ese1_id ) );
assertEquals( Arrays.asList( 1, 2, 3, 4, 5, 6 ), getAuditReader().getRevisions( EmbeddableSetEntity.class, ese1_id ) );
}

@Test
public void testHistoryOfEse1() {
EmbeddableSetEntity rev1 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 1 );
EmbeddableSetEntity rev2 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 2 );
EmbeddableSetEntity rev3 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 3 );

assertEquals( Collections.singleton( c3_1 ), rev1.getComponentSet() );
assertEquals( TestTools.makeSet( c3_1, c3_2 ), rev2.getComponentSet() );
assertEquals( TestTools.makeSet( c3_1 ), rev3.getComponentSet() );
EmbeddableSetEntity rev4 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 4 );
EmbeddableSetEntity rev5 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 5 );
EmbeddableSetEntity rev6 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 6 );

assertEquals( TestTools.makeSet( c3_1, c3_3 ), rev1.getComponentSet() );
assertEquals( TestTools.makeSet( c3_1, c3_2, c3_3 ), rev2.getComponentSet() );
assertEquals( TestTools.makeSet( c3_1, c3_3 ), rev3.getComponentSet() );
assertEquals( TestTools.makeSet( c3_1, c3_2, c3_3, c3_4 ), rev4.getComponentSet() );
assertEquals( TestTools.makeSet( c3_1, c3_3 ), rev5.getComponentSet() );
assertEquals( TestTools.makeSet( c3_2, c3_4 ), rev6.getComponentSet() );
}
}

0 comments on commit 4796553

Please sign in to comment.