Permalink
Browse files

OGM-141 Populate AssociationKey's role, entityKey and association typ…

…e for all collection of embeddable cases
  • Loading branch information...
emmanuelbernard authored and Sanne committed Mar 28, 2012
1 parent bdc1d40 commit 79db2d1c10cc4d4c69544c958389a7ab7a73b902
@@ -404,13 +404,14 @@ private TupleAsMapResultSet getResultSet(Serializable id, SessionImplementor ses
if ( getCollectionPersisters().length != 1 ) {
throw new AssertionFailure( "Found an unexpected number of collection persisters: " + getCollectionPersisters().length );
}
- final CollectionPhysicalModel persister = (CollectionPhysicalModel) getCollectionPersisters()[0];
+ final OgmCollectionPersister persister = (OgmCollectionPersister) getCollectionPersisters()[0];
PropertyMetadataProvider metadataProvider = new PropertyMetadataProvider()
.gridDialect(gridDialect)
.tableName(persister.getTableName())
.key( id )
.keyColumnNames( persister.getKeyColumnNames() )
.keyGridType( persister.getKeyGridType() )
+ .collectionPersister( persister )
.session( session );
Association assoc = metadataProvider.getCollectionMetadataOrNull();
if ( assoc != null ) {
@@ -188,6 +188,7 @@ private void doAddPropertyMetadata(int tableIndex,
.keyColumnNames( persister.getPropertyColumnNames( propertyIndex ) )
.keyColumnValues( newColumnValue )
.session( session )
+ //does not set .collectionPersister as it does not make sense here for a ToOne or a unique key
.tableName( persister.getTableName( tableIndex ) );
Tuple tuple = new Tuple( EmptyTupleSnapshot.SINGLETON );
//add the id column
@@ -221,6 +222,7 @@ private void doRemovePropertyMetadata(int tableIndex,
.keyColumnNames( persister.getPropertyColumnNames( propertyIndex ) )
.keyColumnValues( oldColumnValue )
.session( session )
+ //does not set .collectionPersister as it does not make sense here for a ToOne or a unique key
.tableName( persister.getTableName( tableIndex ) );
Tuple tupleKey = new Tuple( EmptyTupleSnapshot.SINGLETON );
gridIdentifierType.nullSafeSet( tupleKey, id, persister.getIdentifierColumnNames(), session );
@@ -225,6 +225,7 @@ protected int doUpdateRows(Serializable key, PersistentCollection collection, Se
.key( key )
.keyColumnNames( getKeyColumnNames() )
.keyGridType( getKeyGridType() )
+ .collectionPersister( this )
.session( session );
while ( entries.hasNext() ) {
@@ -387,6 +388,7 @@ public int getSize(Serializable key, SessionImplementor session) {
.gridDialect(gridDialect)
.tableName( getTableName() )
.keyGridType( getKeyGridType() )
+ .collectionPersister( this )
.keyColumnNames( getKeyColumnNames() );
final Association collectionMetadata = metadataProvider.getCollectionMetadataOrNull();
@@ -414,6 +416,7 @@ public void deleteRows(PersistentCollection collection, Serializable id, Session
.key( id )
.keyColumnNames( getKeyColumnNames() )
.keyGridType( getKeyGridType() )
+ .collectionPersister( this )
.session( session );
//delete all the deleted entries
@@ -468,6 +471,7 @@ public void insertRows(PersistentCollection collection, Serializable id, Session
.key( id )
.keyColumnNames( getKeyColumnNames() )
.keyGridType( getKeyGridType() )
+ .collectionPersister( this )
.session( session );
//insert all the new entries
@@ -520,6 +524,7 @@ public void recreate(PersistentCollection collection, Serializable id, SessionIm
.key( id )
.keyColumnNames( getKeyColumnNames() )
.keyGridType( getKeyGridType() )
+ .collectionPersister( this )
.session( session );
//create all the new entries
@@ -600,6 +605,7 @@ else if ( associationType == AssociationType.ASSOCIATION_TABLE_TO_ENTITY ) {
.keyColumnNames( elementColumnNames )
.keyColumnValues( elementColumnValues )
.session( session )
+ .collectionPersister( this )
.tableName( getTableName() );
//TODO what happens when a row should be *updated* ?: I suspect ADD works OK as it's a put()
@@ -650,6 +656,7 @@ public void remove(Serializable id, SessionImplementor session) throws Hibernate
.key( id )
.keyColumnNames( getKeyColumnNames() )
.keyGridType( getKeyGridType() )
+ .collectionPersister( this )
.session( session );
//shortcut to avoid loop if we can
@@ -390,6 +390,7 @@ public Object loadByUniqueKey(
.key( uniqueKey )
.keyGridType( gridUniqueKeyType )
.keyColumnNames( getPropertyColumnNames( propertyIndex ) )
+ //does not set .collectionPersister as it does not make sense here for an entity
.session( session );
final Association ids = metadataProvider.getCollectionMetadata();
@@ -25,9 +25,16 @@
import org.hibernate.ogm.datastore.spi.Tuple;
import org.hibernate.ogm.dialect.GridDialect;
import org.hibernate.ogm.grid.AssociationKey;
+import org.hibernate.ogm.grid.AssociationType;
+import org.hibernate.ogm.grid.EntityKey;
import org.hibernate.ogm.grid.RowKey;
+import org.hibernate.ogm.persister.EntityKeyBuilder;
+import org.hibernate.ogm.persister.OgmCollectionPersister;
+import org.hibernate.ogm.persister.OgmEntityPersister;
import org.hibernate.ogm.type.GridType;
+import java.io.Serializable;
+
/**
* @author Emmanuel Bernard
*/
@@ -41,6 +48,7 @@
private Association collectionMetadata;
private Object[] columnValues;
private GridDialect gridDialect;
+ private OgmCollectionPersister collectionPersister;
//fluent methods for populating data
@@ -87,6 +95,19 @@ private AssociationKey getCollectionMetadataKey() {
if ( collectionMetadataKey == null ) {
final Object[] columnValues = getKeyColumnValues();
collectionMetadataKey = new AssociationKey( tableName, keyColumnNames, columnValues );
+ if (collectionPersister != null) {
+ collectionMetadataKey.setCollectionRole( collectionPersister.getRole() );
+ EntityKey entityKey = EntityKeyBuilder.fromPersister(
+ (OgmEntityPersister) collectionPersister.getOwnerEntityPersister(),
+ (Serializable) key,
+ session
+ );
+ collectionMetadataKey.setOwnerEntityKey( entityKey );
+ //TODO add information on the collection type, set, map, bag, list etc
+
+ AssociationType type = collectionPersister.getElementType().isEntityType() ? AssociationType.ASSOCIATION : AssociationType.EMBEDDED;
+ collectionMetadataKey.setAssociationType( type );
+ }
}
return collectionMetadataKey;
}
@@ -132,4 +153,9 @@ public void flushToCache() {
gridDialect.updateAssociation( getCollectionMetadata(), getCollectionMetadataKey() );
}
}
+
+ public PropertyMetadataProvider collectionPersister(OgmCollectionPersister collectionPersister) {
+ this.collectionPersister = collectionPersister;
+ return this;
+ }
}

0 comments on commit 79db2d1

Please sign in to comment.