Skip to content

Commit

Permalink
OGM-914 Exposing entity key metadata through AssociationKeyMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Sep 1, 2015
1 parent 255827b commit 60ed0dc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.hibernate.ogm.model.key.spi.AssociationKeyMetadata;
import org.hibernate.ogm.model.key.spi.AssociationKind;
import org.hibernate.ogm.model.key.spi.AssociationType;
import org.hibernate.ogm.model.key.spi.EntityKeyMetadata;
import org.hibernate.ogm.util.impl.ArrayHelper;

/**
Expand All @@ -32,6 +33,7 @@ public class DefaultAssociationKeyMetadata implements AssociationKeyMetadata {
private final String[] rowKeyColumnNames;
private final String[] rowKeyIndexColumnNames;
private final boolean isInverse;
private final EntityKeyMetadata entityKeyMetadata;
private final AssociatedEntityKeyMetadata associatedEntityKeyMetadata;
private final String collectionRole;
private final AssociationKind associationKind;
Expand All @@ -44,6 +46,7 @@ private DefaultAssociationKeyMetadata(Builder builder) {
this.rowKeyColumnNames = builder.rowKeyColumnNames;
this.rowKeyIndexColumnNames = builder.rowKeyIndexColumnNames;
this.isInverse = builder.isInverse;
this.entityKeyMetadata = builder.entityKeyMetadata;
this.associatedEntityKeyMetadata = builder.associatedEntityKeyMetadata;
this.collectionRole = builder.collectionRole;
this.associationKind = builder.associationKind;
Expand All @@ -60,6 +63,7 @@ public static class Builder {
private String[] rowKeyColumnNames;
private String[] rowKeyIndexColumnNames = ArrayHelper.EMPTY_STRING_ARRAY;
private boolean isInverse;
private EntityKeyMetadata entityKeyMetadata;
private AssociatedEntityKeyMetadata associatedEntityKeyMetadata;
private String collectionRole;
private AssociationKind associationKind;
Expand Down Expand Up @@ -90,6 +94,11 @@ public Builder inverse(boolean isInverse) {
return this;
}

public Builder entityKeyMetadata(EntityKeyMetadata entityKeyMetadata) {
this.entityKeyMetadata = entityKeyMetadata;
return this;
}

public Builder associatedEntityKeyMetadata(AssociatedEntityKeyMetadata associatedEntityKeyMetadata) {
this.associatedEntityKeyMetadata = associatedEntityKeyMetadata;
return this;
Expand Down Expand Up @@ -144,6 +153,11 @@ public String[] getRowKeyIndexColumnNames() {
return rowKeyIndexColumnNames;
}

@Override
public EntityKeyMetadata getEntityKeyMetadata() {
return entityKeyMetadata;
}

/**
* Returns meta-data about the entity key referenced by associations of this key family.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public interface AssociationKeyMetadata {
*/
String[] getRowKeyIndexColumnNames();

/**
* Returns meta-data about the entity-key on this side of associations of this key family.
*
* @return meta-data about the entity-key on this side of associations of this key family.
*/
EntityKeyMetadata getEntityKeyMetadata();

/**
* Returns meta-data about the entity key referenced by associations of this key family.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ else if ( getElementType().isAssociationType() && getElementType().isEntityType(
.columnNames( getKeyColumnNames() )
.rowKeyColumnNames( rowKeyColumnNames )
.rowKeyIndexColumnNames( rowKeyIndexColumnNames )
.entityKeyMetadata( ( (OgmEntityPersister) getOwnerEntityPersister() ).getEntityKeyMetadata() )
.associatedEntityKeyMetadata( new DefaultAssociatedEntityKeyMetadata( getElementColumnNames(), targetEntityKeyMetadata( false ) ) )
.inverse( isInverse )
.collectionRole( getUnqualifiedRole() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ private Map<String, AssociationKeyMetadata> initInverseOneToOneAssociationKeyMet
.table( getTableName() )
.columnNames( propertyColumnNames )
.rowKeyColumnNames( rowKeyColumnNames )
.entityKeyMetadata( otherSidePersister.getEntityKeyMetadata() )
.associatedEntityKeyMetadata( new DefaultAssociatedEntityKeyMetadata( entityKeyMetadata.getColumnNames(), entityKeyMetadata ) )
.inverse( true )
.collectionRole( inverseOneToOneProperty )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.hibernate.ogm.backendtck.associations.collection.types.Race;
import org.hibernate.ogm.backendtck.associations.collection.types.Runner;
import org.hibernate.ogm.backendtck.associations.collection.types.User;
import org.hibernate.ogm.backendtck.associations.manytoone.SalesForce;
import org.hibernate.ogm.backendtck.associations.manytoone.SalesGuy;
import org.hibernate.ogm.backendtck.associations.onetoone.Husband;
import org.hibernate.ogm.backendtck.associations.onetoone.Wife;
import org.hibernate.ogm.backendtck.embeddable.MultiAddressAccount;
Expand Down Expand Up @@ -50,6 +52,21 @@ public void testAssociationType() {
assertThat( akm.getAssociationType() ).isEqualTo( AssociationType.ONE_TO_ONE );
}

@Test
public void testEntityKeyMetadata() {
AssociationKeyMetadata akm = getCollectionPersister( BankAccount.class.getName() + ".owners" ).getAssociationKeyMetadata();
assertThat( akm.getEntityKeyMetadata().getTable() ).isEqualTo( "BankAccount" );

akm = getCollectionPersister( AccountOwner.class.getName() + ".bankAccounts" ).getAssociationKeyMetadata();
assertThat( akm.getEntityKeyMetadata().getTable() ).isEqualTo( "AccountOwner" );

akm = getCollectionPersister( SalesForce.class.getName() + ".salesGuys" ).getAssociationKeyMetadata();
assertThat( akm.getEntityKeyMetadata().getTable() ).isEqualTo( "SalesForce" );

akm = getEntityPersister( Husband.class.getName() ).getInverseOneToOneAssociationKeyMetadata( "wife" );
assertThat( akm.getEntityKeyMetadata().getTable() ).isEqualTo( "Wife" );
}

private OgmEntityPersister getEntityPersister(String entityName) {
return (OgmEntityPersister) ( sfi() ).getEntityPersister( entityName );
}
Expand All @@ -61,6 +78,6 @@ private OgmCollectionPersister getCollectionPersister(String role) {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { BankAccount.class, AccountOwner.class, Husband.class, Wife.class, MultiAddressAccount.class, Address.class, Race.class,
Runner.class, User.class, PhoneNumber.class };
Runner.class, User.class, PhoneNumber.class, SalesForce.class, SalesGuy.class };
}
}

0 comments on commit 60ed0dc

Please sign in to comment.