NHibernate映射:UserTypes与多对一 [英] NHibernate mapping: UserTypes with many-to-one

查看:175
本文介绍了NHibernate映射:UserTypes与多对一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚接触NHibernate并学习它,因为我们正在修改现有的解决方案来使用这个ORM。
理想情况下,存储结构和对象类需要保持不变,所以我遇到了一个或两个映射问题。

一个类Money价值和货币。这个值是一个double值,这个货币是一个货币列表表的外键。

货币可以在许多对象/表中显示为一个类型,所以我创建了一个CompositeUserType将其与标准映射一起映射到货币。
这工作正常,但对于我的生活,我不能从货币关系缓慢加载从NHibernate。我们使用流利的,但很高兴的任何在hbm指针。

解决方案

看起来像我需要使用组件,看不到但是如何从这些或者CompositeUserTypes中添加引用:/ b / b
$ b

https://forum.hibernate.org/viewtopic.php?f=1&t=947719&start=0



https://web.archive.org/web/20090227235136/http://blogs.hibernatingrhinos.com /nhibernate/archive/2008/08/13/a-fluent-interface-to-nhibernate---part-2---value.aspx



http://wiki.fluentnhibernate.org/show/StandardMappingComponents



UPDATE

我通过使用CompositeUserType并设置了代理端点NullSafeGet()方法:

  public virtual object NullSafeGet(IDataReader dr,string [] names,ISessionImplementor session,object owner) b $ b {
if(dr == null)
{
return null;



















$ )
};

string entityName = session.BestGuessEntityName(value.Currency);
value.Currency =(CurrencyDetails)session.InternalLoad(entityName,(object)DEFAULT_CURRENCY_ID,false,false);

返回值;





$ b不知道这是否是推荐的方式, )

New to NHibernate and learning it as we are modifying an existing solution to use this ORM. Ideally, the storage structure and object classes need to stay the same, so Ive come across one or two mapping problems.

One class 'Money' has a value and currency. The value is a double and the currency is a foreign key to a list table of currencies.

Money can appear as a type on many objects/tables, so Ive created a CompositeUserType to map it along with a standard mapping to currency. This works fine, but for the life of me I cannot get the currency relationship to lazy load from NHibernate. We use fluent, but am happy for any pointers in hbm.

解决方案

Looks like I need to use components, can't see how to add references from those or CompositeUserTypes, though :/

https://forum.hibernate.org/viewtopic.php?f=1&t=947719&start=0

https://web.archive.org/web/20090227235136/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/08/13/a-fluent-interface-to-nhibernate---part-2---value.aspx

http://wiki.fluentnhibernate.org/show/StandardMappingComponents

UPDATE

I have got round this issue by using a CompositeUserType and setting up the proxy endpoint on the entity diring the NullSafeGet() method:

public virtual object NullSafeGet(IDataReader dr, string[] names, ISessionImplementor session, object owner)
{
    if (dr == null)
    {
        return null;
    }

    Money value = new Money()
    {
        Value = (double)NHibernateUtil.Double.NullSafeGet(dr, names[0], session, owner)
    };

    string entityName = session.BestGuessEntityName(value.Currency);
    value.Currency = (CurrencyDetails)session.InternalLoad(entityName, (object)DEFAULT_CURRENCY_ID, false, false);

    return value;
}

Not sure if this is the recommended way of doing it, but it works :)

这篇关于NHibernate映射:UserTypes与多对一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆