如何从IronPython配置NHibernate类映射?

时间:2009-12-24 19:48:32

标签: nhibernate ironpython

我尝试使用IronPython中的NHibernate。我已将NHibernate依赖项复制到我的C:\ Users \ shamel \ python \ HelloPy \ libs目录。

我有这个IronPython代码:

import sys
sys.path.append("C:\Users\shamel\python\HelloPy\libs") 

import clr
clr.AddReference("NHibernate")
clr.AddReference("LinFu.DynamicProxy")
clr.AddReference("NHibernate.ByteCode.LinFu")

from Entities.Entity import Customer 
from NHibernate.Cfg import Configuration

configuration = Configuration();
configuration.Configure("PocoLib.cfg.xml");
configuration.AddXmlFile("Customer.hbm.xml");
factory = configuration.BuildSessionFactory();

我的实体类在Entity包中的Entity.py中定义。这解释了from Entities.Entity import Customer

这是我的Customer.hbm.xml文件的样子

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="Entities"
               namespace="Entities">

<class name="Customer" lazy="false">
...
</class>

程序在configuration.AddXmlFile(“Customer.hbm.xml”)上失败;行:

configuration.AddXmlFile("Customer.hbm.xml");
StandardError: Could not compile the mapping document: Customer.hbm.xml
ERROR: Module: nhtests could not be imported.

我认为NHibernate无法解析assembly标头中指定的namespacehibernate-mapping(问题不在于找不到xml文件;我尝试使用不存在的名称,我收到了不同的错误)

非常感谢任何帮助。

狡猾

编辑:2009年12月27日:这是堆栈跟踪。我认为这证实了NHibernate无法找到我的类型。

*** Outer ***
MappingException
persistent class Entities.Customer, Entities not found
at Microsoft.Scripting.Actions.Calls.MethodCandidate.Caller.CallWithInstance(Object[] args, Boolean& shouldOptimize)
at IronPython.Runtime.Types.BuiltinFunction.BuiltinMethodCaller`2.Call1(CallSite site, CodeContext context, TFuncType func, T0 arg0)
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at Microsoft.Scripting.Utils.InvokeHelper`6.Invoke(Object arg0, Object arg1, Object arg2, Object arg3, Object arg4)
at Microsoft.Scripting.Utils.ReflectedCaller.Invoke(Object[] args)
at Microsoft.Scripting.Interpreter.CallInstruction.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
*** Inner ***
MappingException
persistent class Entities.Customer, Entities not found
at NHibernate.Cfg.XmlHbmBinding.Binder.ClassForFullNameChecked(String fullName, String errorMessage)
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindClass(XmlNode node, IDecoratable classMapping, PersistentClass model, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(XmlNode node, HbmClass classSchema, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(XmlNode parentNode, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(XmlNode node)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
*** Inner Inner ***
FileNotFoundException
Could not load file or assembly 'Entities' or one of its dependencies. The system cannot find the file specified.
at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at NHibernate.Util.ReflectHelper.TypeFromAssembly(AssemblyQualifiedTypeName name, Boolean throwOnError)
at NHibernate.Util.ReflectHelper.ClassForName(String name)
at NHibernate.Cfg.XmlHbmBinding.Binder.ClassForFullNameChecked(String fullName, String errorMessage)

编辑2009年12月29日: 即使我解决了命名空间/程序集名称问题,我也不认为Nhibernate能够保存/加载我的Python实体。就现在的理解而言,当Python实例传递给静态类型库(例如NHibernate)时,该库不会看到它的成员。因此Nhibernate将不会在我的类型上找到任何属性,因为这些属性对于静态类型库是不可见的。为了实现这一点,NHibernate必须支持DLR集成,可能使用C#4.0 Dynamic关键字。继续......

1 个答案:

答案 0 :(得分:1)

在IronPython中定义的实体上无法使用IronPython中的NHibernate。