本文介绍了测试集合是多对多还是多对一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无论如何,我是否有必要测试集合在会话中是多对多还是多对多关系.
Is there anyway for me to test if a collection is a many to many or many to one relationship from the session.
我已经尽量使用
SessionFactory.GetClassMetadata(type)
SessionFactory.GetClassMetadata(type)
提供了PropertyTypes集合,但这并不能区分多对多和多对一的情况,
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
推荐答案
以下代码将迭代TheEntity
属性名称,以查找映射的集合.然后,它将要求Collection持久性并获得有关一对多或多对多的信息
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;
}
注意:我想在您的问题中,多对一(这是参考而非集合映射)应该为One-to-Many
这篇关于测试集合是多对多还是多对一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!