NHibernate mapping: UserTypes with many-to-one

独自空忆成欢 提交于 2019-12-12 10:11:43

问题


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.


回答1:


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 :)



来源:https://stackoverflow.com/questions/1102233/nhibernate-mapping-usertypes-with-many-to-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!