简体   繁体   English

nhibernate Composite-id,不存在键多对一记录

[英]nhibernate composite-id with not existing key-many-to-one record

i have old legacy DB which has dead links in their tables. 我有旧的遗留数据库,它们的表中有无效链接。 I have class mapped in nhibernate like this: 我在nhibernate中映射了这样的类:

<class name="Visible" table="table_visible">
    <composite-id>
        <key-many-to-one column="object_id" name="ObjectA" />
        <key-many-to-one column="sub_object_id" name="SubObject" />
    </composite-id>
    <property column="visible" name="VisibleRow" />
</class>

and: 和:

public class Visible
{
    public virtual ObjectAClass ObjectA { get; set; }
    public virtual SubObjectClass SubObject { get; set; }
    public virtual bool VisibleRow { get; set; }

    public override bool Equals(object obj)
    {
        var other = ((Visible)obj);
        return this.ObjectA.Equals(other.ObjectA) && this.SubObject.Equals(other.SubObject);
    }

    public override int GetHashCode()
    {
        return this.ObjectA.GetHashCode() + (this.SubObject != null? this.SubObject.GetHashCode(): 0);
    }
}

Now all works fine when all joins in database are correct, but when i find such sub_object_id which doesnt have entity, nhibernate throws me error 现在,当数据库中的所有联接正确时,所有方法都可以正常工作,但是当我找到没有实体的sub_object_id时,nhibernate会抛出错误

No row with the given identifier exists:[SubObject#123]

Is there a way to map composite key so that when its subentity is not found, the whole entity wouldnt be loaded (like with inner join)? 有没有办法映射复合键,以便在找不到其子实体时不会加载整个实体(就像内部联接一样)?

NHibernate v2.0.50727 NHibernate v2.0.50727

Following Daniel Schilling idea of fetch Visible entities with a where exists sub-query, found that there is loader element available in mappings. 遵循Daniel Schilling的想法,其中使用where exist子查询来获取Visible实体,发现映射中有可用的loader元素。

<class name="ObjectA" table="table_object">
    .........   
    <set name="VisibleList" cascade="all" lazy="false" inverse="true">
        <key column="object_id" />
        <one-to-many class="Visible" />
        <loader query-ref="valid_entities"/>
    </set>

</class>
<sql-query name="valid_entities">
    <load-collection alias="v" role="ObjectA.VisibleList"/>
    SELECT {v.*}
    FROM table_visible v
    INNER JOIN table_sub_entities e ON e.sub_entity_id=v.sub_entity_id
    WHERE v.object_id=?
</sql-query>

And nothing else needed to be changed. 无需更改任何其他内容。

<key-many-to-one column="sub_object_id" name="SubObject" not-found="ignore" />

... may be helpful. ...可能会有所帮助。 From the NHibernate Documentation ... NHibernate文档中 ...

ignore will treat a missing row as a null association ignore会将缺失的行视为空关联

Please be aware of the performance penalty associated with using this option. 请注意与使用此选项相关的性能损失。 Whenever NHibernate fetches a Visible entity, it will also have to fetch SubObject . 每当NHibernate获取Visible实体时,它也必须获取SubObject If you don't go ahead and fetch it in your query, this means that NHibernate will be issuing lots of lazy loads. 如果您不继续在查询中获取它,则意味着NHibernate将发出大量延迟加载。

This doesn't meet your "when its sub-entity is not found, the whole entity wouldn't be loaded" goal. 这不符合您的“当找不到其子实体时,将不会加载整个实体”的目标。 Instead NHibernate would give you an entity with a null sub-entity. 相反,NHibernate将为您提供一个具有空子实体的实体。 If you want that inner-join-like behavior, then I think you would need to fetch your Visible entities with a where exists sub-query to make sure the SubObject actually exists. 如果您想要这种类似于内部SubObject行为,那么我认为您需要使用where exists子查询来获取Visible实体,以确保SubObject实际上存在。

The best option would be to fix the data in the database and add a foreign key constraint. 最好的选择是修复数据库中的数据并添加外键约束。

I just ran across this: Relations with not-found="ignore" . 我只是碰到了这一点: 与not-found =“ ignore”的关系 I promise I'm not copying Ricci's content - I'm writing this from my own experience. 我保证我不会复制Ricci的内容-我是根据自己的经验编写的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM