Reputation: 434
Is there anyway for me to test if a collection is a many to many or many to one relationship from the session.
I have got as far as using the
SessionFactory.GetClassMetadata(type)
which gives the PropertyTypes collection but this doesn't distinguish between many to many and many to one where as one to many is catered for
Upvotes: 2
Views: 179
Reputation: 123861
The below code, will iterate TheEntity
property names, to find the mapped collections. Then, it will ask for Collection persister and get informed about One-to-Many or Many-to-Many
var persister = factory
.GetClassMetadata(typeof(TheEntity)) as AbstractEntityPersister;
// iterate property names
foreach (var propertyName in persister.PropertyNames)
{
// find type
var index = persister.GetPropertyIndex(propertyName);
var propertyType = persister.PropertyTypes[index];
// check if it is collection
if (!propertyType.IsCollectionType)
{
continue;
}
// the Role is
// the Entity type Name & the Collection name
var roleName = persister.Name + "." + propertyName;
// the Abstract collection persister
var collectionPersister = factory
.GetCollectionMetadata(roleName) as AbstractCollectionPersister;
// here we go:
var isManyToMany = collectionPersister.IsManyToMany;
var isOneToMany = collectionPersister.IsOneToMany;
}
Note: I guess that in your question the Many-to-One (which is the reference not collection mapping) should be One-to-Many
Upvotes: 1