nhibernate - 如何从 IronPython 配置 NHibernate 类映射?

标签 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>

程序在配置上失败。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 无法解决 assemblynamespacehibernate-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 日 :
即使在我解决了命名空间/程序集名称问题之后,我也不认为 N​​hibernate 能够保存/加载我的 Python 实体。就现在的理解,当一个 Python 实例被传递给一个静态类型库(比如 NHibernate)时,那个库看不到它的成员。因此 Nhibernate 不会在我的类型上找到任何属性,因为这些属性对静态类型库不可见。为了使其工作,NHibernate 必须支持 DLR 集成,可能使用 C# 4.0 Dynamic 关键字。未完待续...

最佳答案

不可能在 IronPython 中定义的实体上使用来自 IronPython 的 NHibernate。

关于nhibernate - 如何从 IronPython 配置 NHibernate 类映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1959534/

相关文章:

c# - Ironpython:IEnumerator

python - 从 Python 代码构建 .NET DLL?

python - 已编译 Python 与 IronPython 的启动时间

javascript - 是否可以在 spotfire 文本区域显示图像/消息以等待数据加载?

nhibernate - 为什么 NHibernate 不先删除孤儿呢?

oracle - 无法执行 native 批量操作查询

NHibernate Criteria.AddOrder(Order.Asc ("Column.Property"))

c# - 使用 ORM 时的命令/查询分离

c# - 我如何在 NHibernate 中进行通用字段和组件级更改跟踪?

python - IronPython 是 100% 纯 Python 变体吗?