HHH-8171 - Cleanup

This commit is contained in:
Lukasz Antoniak 2013-04-14 22:25:46 +02:00
parent 4796553a3f
commit f2b9ab8278
7 changed files with 36 additions and 45 deletions

View File

@ -323,7 +323,7 @@
SETORDINAL SETORDINAL
</entry> </entry>
<entry> <entry>
The name of the column used for storing the ordinal of the change in sets of embeddables. Name of column used for storing ordinal of the change in sets of embeddable elements.
</entry> </entry>
</row> </row>
</tbody> </tbody>

View File

@ -101,7 +101,7 @@ public interface EnversSettings {
public static final String AUDIT_STRATEGY_VALIDITY_REVEND_TIMESTAMP_FIELD_NAME = "org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name"; 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}. * Name of column used for storing ordinal of the change in sets of embeddable elements. Defaults to {@literal SETORDINAL}.
*/ */
public static final String EMBEDDABLE_SET_ORDINAL_FIELD_NAME = "org.hibernate.envers.embeddable_set_ordinal_field_name"; public static final String EMBEDDABLE_SET_ORDINAL_FIELD_NAME = "org.hibernate.envers.embeddable_set_ordinal_field_name";
} }

View File

@ -104,7 +104,8 @@ public class AuditEntitiesConfiguration {
revisionPropBasePath = originalIdPropName + "." + revisionFieldName + "."; revisionPropBasePath = originalIdPropName + "." + revisionFieldName + ".";
embeddableSetOrdinalPropertyName = ConfigurationHelper.getString( embeddableSetOrdinalPropertyName = ConfigurationHelper.getString(
EnversSettings.EMBEDDABLE_SET_ORDINAL_FIELD_NAME, properties, "SETORDINAL" ); EnversSettings.EMBEDDABLE_SET_ORDINAL_FIELD_NAME, properties, "SETORDINAL"
);
} }
public String getOriginalIdPropName() { public String getOriginalIdPropName() {

View File

@ -88,7 +88,6 @@ import org.hibernate.mapping.ManyToOne;
import org.hibernate.mapping.OneToMany; import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property; import org.hibernate.mapping.Property;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table; import org.hibernate.mapping.Table;
import org.hibernate.mapping.Value; import org.hibernate.mapping.Value;
import org.hibernate.type.BagType; import org.hibernate.type.BagType;
@ -507,20 +506,16 @@ public final class CollectionMetadataGenerator {
); );
} }
// Add an additional column holding a number to make each entry unique within the set, // Add an additional column holding a number to make each entry unique within the set.
// since embeddable properties can be null // Embeddable properties may contain null values, so cannot be stored within composite primary key.
if ( propertyValue.getCollectionType() instanceof SetType ) { if ( propertyValue.isSet() ) {
final String auditedEmbeddableSetOrdinalPropertyName = mainGenerator.getVerEntCfg() final String setOrdinalPropertyName = mainGenerator.getVerEntCfg().getEmbeddableSetOrdinalPropertyName();
.getEmbeddableSetOrdinalPropertyName(); final Element ordinalProperty = MetadataTools.addProperty(
final String auditedEmbeddableSetOrdinalPropertyType = "integer"; xmlMapping, setOrdinalPropertyName, "integer", true, true
);
final SimpleValue simpleValue = new SimpleValue( component.getMappings(), component.getTable() ); MetadataTools.addColumn(
simpleValue.setTypeName( auditedEmbeddableSetOrdinalPropertyType ); ordinalProperty, setOrdinalPropertyName, null, null, null, null, null, null, false
);
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 ); return new MiddleComponentData( componentMapper, 0 );

View File

@ -87,27 +87,16 @@ public abstract class AbstractCollectionMapper<T> implements PropertyMapper {
protected abstract void mapToMapFromObject(SessionImplementor session, Map<String, Object> idData, Map<String, Object> data, Object changed); protected abstract void mapToMapFromObject(SessionImplementor session, Map<String, Object> idData, Map<String, Object> data, Object changed);
/** /**
* Creates a Map for the id. * Creates map for storing identifier data. Ordinal parameter guarantees uniqueness of primary key.
* * Composite primary key cannot contain embeddable properties since they might be nullable.
* <p> * @param ordinal Iteration ordinal.
* The ordinal parameter represents the iteration ordinal of the current element, used to add a synthetic id when * @return Map for holding identifier data.
* 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) { protected Map<String, Object> createIdMap(int ordinal) {
final HashMap<String, Object> idMap = new HashMap<String, Object>(); final Map<String, Object> idMap = new HashMap<String, Object>();
if ( ordinalInId ) { if ( ordinalInId ) {
idMap.put( this.commonCollectionMapperData.getVerEntCfg().getEmbeddableSetOrdinalPropertyName(), idMap.put( commonCollectionMapperData.getVerEntCfg().getEmbeddableSetOrdinalPropertyName(), ordinal );
Integer.valueOf( ordinal ) );
} }
return idMap; return idMap;
} }

View File

@ -41,8 +41,7 @@ public final class SortedSetCollectionMapper extends BasicCollectionMapper<Sorte
Class<? extends SortedSet> collectionClass, Class<? extends SortedSet> proxyClass, Class<? extends SortedSet> collectionClass, Class<? extends SortedSet> proxyClass,
MiddleComponentData elementComponentData, Comparator comparator, boolean ordinalInId, MiddleComponentData elementComponentData, Comparator comparator, boolean ordinalInId,
boolean revisionTypeInId) { boolean revisionTypeInId) {
super( commonCollectionMapperData, collectionClass, proxyClass, elementComponentData, ordinalInId, super( commonCollectionMapperData, collectionClass, proxyClass, elementComponentData, ordinalInId, revisionTypeInId );
revisionTypeInId );
this.comparator = comparator; this.comparator = comparator;
} }

View File

@ -24,7 +24,6 @@
package org.hibernate.envers.test.integration.collection.embeddable; package org.hibernate.envers.test.integration.collection.embeddable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import org.junit.Test; import org.junit.Test;
@ -65,7 +64,7 @@ public class EmbeddableSet extends BaseEnversJPAFunctionalTestCase {
EmbeddableSetEntity ese1 = new EmbeddableSetEntity(); EmbeddableSetEntity ese1 = new EmbeddableSetEntity();
// Revision 1 (ese1: initially 2 elements) // Revision 1 (ese1: initially two elements)
em.getTransaction().begin(); em.getTransaction().begin();
ese1.getComponentSet().add( c3_1 ); ese1.getComponentSet().add( c3_1 );
ese1.getComponentSet().add( c3_3 ); ese1.getComponentSet().add( c3_3 );
@ -84,33 +83,33 @@ public class EmbeddableSet extends BaseEnversJPAFunctionalTestCase {
ese1.getComponentSet().add( c3_2 ); ese1.getComponentSet().add( c3_2 );
em.getTransaction().commit(); em.getTransaction().commit();
// Revision 3 (ese1: adding one existing element) // Revision (still 2) (ese1: adding one existing element)
em.getTransaction().begin(); em.getTransaction().begin();
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() ); ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
ese1.getComponentSet().add( c3_1 ); ese1.getComponentSet().add( c3_1 );
em.getTransaction().commit(); em.getTransaction().commit();
// Revision 4 (ese1: removing one existing element) // Revision 3 (ese1: removing one existing element)
em.getTransaction().begin(); em.getTransaction().begin();
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() ); ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
ese1.getComponentSet().remove( c3_2 ); ese1.getComponentSet().remove( c3_2 );
em.getTransaction().commit(); em.getTransaction().commit();
// Revision 5 (ese1: adding two elements) // Revision 4 (ese1: adding two elements)
em.getTransaction().begin(); em.getTransaction().begin();
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() ); ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
ese1.getComponentSet().add( c3_2 ); ese1.getComponentSet().add( c3_2 );
ese1.getComponentSet().add( c3_4 ); ese1.getComponentSet().add( c3_4 );
em.getTransaction().commit(); em.getTransaction().commit();
// Revision 6 (ese1: removing two elements) // Revision 5 (ese1: removing two elements)
em.getTransaction().begin(); em.getTransaction().begin();
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() ); ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
ese1.getComponentSet().remove( c3_2 ); ese1.getComponentSet().remove( c3_2 );
ese1.getComponentSet().remove( c3_4 ); ese1.getComponentSet().remove( c3_4 );
em.getTransaction().commit(); em.getTransaction().commit();
// Revision 7 (ese1: removing and adding two elements) // Revision 6 (ese1: removing and adding two elements)
em.getTransaction().begin(); em.getTransaction().begin();
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() ); ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
ese1.getComponentSet().remove( c3_1 ); ese1.getComponentSet().remove( c3_1 );
@ -119,6 +118,12 @@ public class EmbeddableSet extends BaseEnversJPAFunctionalTestCase {
ese1.getComponentSet().add( c3_4 ); ese1.getComponentSet().add( c3_4 );
em.getTransaction().commit(); em.getTransaction().commit();
// Revision 7 (ese1: adding one element)
em.getTransaction().begin();
ese1 = em.find( EmbeddableSetEntity.class, ese1.getId() );
ese1.getComponentSet().add( c3_1 );
em.getTransaction().commit();
ese1_id = ese1.getId(); ese1_id = ese1.getId();
em.close(); em.close();
@ -126,7 +131,7 @@ public class EmbeddableSet extends BaseEnversJPAFunctionalTestCase {
@Test @Test
public void testRevisionsCounts() { public void testRevisionsCounts() {
assertEquals( Arrays.asList( 1, 2, 3, 4, 5, 6 ), getAuditReader().getRevisions( EmbeddableSetEntity.class, ese1_id ) ); assertEquals( Arrays.asList( 1, 2, 3, 4, 5, 6, 7 ), getAuditReader().getRevisions( EmbeddableSetEntity.class, ese1_id ) );
} }
@Test @Test
@ -137,6 +142,7 @@ public class EmbeddableSet extends BaseEnversJPAFunctionalTestCase {
EmbeddableSetEntity rev4 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 4 ); EmbeddableSetEntity rev4 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 4 );
EmbeddableSetEntity rev5 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 5 ); EmbeddableSetEntity rev5 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 5 );
EmbeddableSetEntity rev6 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 6 ); EmbeddableSetEntity rev6 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 6 );
EmbeddableSetEntity rev7 = getAuditReader().find( EmbeddableSetEntity.class, ese1_id, 7 );
assertEquals( TestTools.makeSet( c3_1, c3_3 ), rev1.getComponentSet() ); 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_2, c3_3 ), rev2.getComponentSet() );
@ -144,5 +150,6 @@ public class EmbeddableSet extends BaseEnversJPAFunctionalTestCase {
assertEquals( TestTools.makeSet( c3_1, c3_2, c3_3, c3_4 ), rev4.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_1, c3_3 ), rev5.getComponentSet() );
assertEquals( TestTools.makeSet( c3_2, c3_4 ), rev6.getComponentSet() ); assertEquals( TestTools.makeSet( c3_2, c3_4 ), rev6.getComponentSet() );
assertEquals( TestTools.makeSet( c3_2, c3_4, c3_1 ), rev7.getComponentSet() );
} }
} }