连接映射内的 HasMany 关系

发布于 2024-08-25 10:44:40 字数 915 浏览 3 评论 0原文

所以,我在流畅的 nhibernate 中映射时遇到问题。我想使用联接映射来展平中间表: 这是我的结构:

[Vehicle]
VehicleId
...

[DTVehicleValueRange]
VehicleId
DTVehicleValueRangeId
AverageValue
...

[DTValueRange]
DTVehicleValueRangeId
RangeMin
RangeMax
RangeValue

请注意,DTValueRange 没有 VehicleID。我想将 DTVehicleValueRange 展平到我的 Vehicle 类中。 Tgis 对于 AverageValue 工作得很好,因为它只是一个普通值,但我似乎无法正确映射 ValueRange 集合。

    public VehicleMap()
    {
        Id(x => x.Id, "VehicleId");
        Join("DTVehicleValueRange", x =>
        {
            x.Optional();
            x.KeyColumn("VehicleId");
            x.Map(y => y.AverageValue).ReadOnly();
            x.HasMany(y => y.ValueRanges).KeyColumn("DTVehicleValueRangeId"); // This Guy
        });
    }

如果 HasMany 映射位于 Join 内部,则它似乎不会执行任何操作。如果它位于 Join 之外并且我指定了表,它会进行映射,但 nhibernate 会尝试使用 VehicleID,而不是 DTVehicleValueRangeId。

我做错了什么?

So, I'm having a problem mapping in fluent nhibernate. I want to use a join mapping to flatten an intermediate table: Here's my structure:

[Vehicle]
VehicleId
...

[DTVehicleValueRange]
VehicleId
DTVehicleValueRangeId
AverageValue
...

[DTValueRange]
DTVehicleValueRangeId
RangeMin
RangeMax
RangeValue

Note that DTValueRange does not have a VehicleID. I want to flatten DTVehicleValueRange into my Vehicle class. Tgis works fine for AverageValue, since it's just a plain value, but I can't seem to get a ValueRange collection to map correctly.

    public VehicleMap()
    {
        Id(x => x.Id, "VehicleId");
        Join("DTVehicleValueRange", x =>
        {
            x.Optional();
            x.KeyColumn("VehicleId");
            x.Map(y => y.AverageValue).ReadOnly();
            x.HasMany(y => y.ValueRanges).KeyColumn("DTVehicleValueRangeId"); // This Guy
        });
    }

The HasMany mapping doesn't seem to do anything if it's inside the Join. If it's outside the Join and I specify the table, it maps, but nhibernate tries to use the VehicleID, not the DTVehicleValueRangeId.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-09-01 10:44:48

今天使用地图创建视图时遇到了类似的问题。生成的 SQL 显示它尝试执行 HasMany<> 操作。在基于 ParentThing 而不是 WorkThing 的 Id 的连接内部(与您遇到的问题相同)

经过多次头到桌映射后,结果将 propertyref 添加到 hasmany 上解决了这个问题。

public class ThingMap : ClassMap<WorkThingView> { 
     public ThingMap() {
                ReadOnly();
                Table("ParentThing");
                Id(x => x.ParentThingId);
                Map(x => x.ParentName);
                Join("WorkThing", join => {
                    join.KeyColumn("ParentThingId");
                    join.Map(m => m.FooCode);
                    join.Map(m => m.BarCode);
                    join.Map(x => x.WorkThingId);
                    join.HasMany(x => x.WorkThingCodes)
                        .Table("WorkThingCode").KeyColumn("WorkThingId").PropertyRef("WorkThingId")
                        .Element("WorkThingCode");
                });
            }
}

Ran into a similar issue today using a Map to create a view. The SQL generated showed it trying to do the HasMany<> inside the join based on the Id of the ParentThing and not WorkThing (same problem you were having)

After much mapping of head-to-desk it turns out adding the propertyref onto the hasmany solved it.

public class ThingMap : ClassMap<WorkThingView> { 
     public ThingMap() {
                ReadOnly();
                Table("ParentThing");
                Id(x => x.ParentThingId);
                Map(x => x.ParentName);
                Join("WorkThing", join => {
                    join.KeyColumn("ParentThingId");
                    join.Map(m => m.FooCode);
                    join.Map(m => m.BarCode);
                    join.Map(x => x.WorkThingId);
                    join.HasMany(x => x.WorkThingCodes)
                        .Table("WorkThingCode").KeyColumn("WorkThingId").PropertyRef("WorkThingId")
                        .Element("WorkThingCode");
                });
            }
}
对你的占有欲 2024-09-01 10:44:47

您能否解释一下 DTVehicleValueRange 表中的平均值列?这不是一个计算值(即不需要保留它)吗?

看起来 VehicleDTValueRange 之间存在多对多关系,当然不会使用联接进行映射,而是使用 HasManyToMany< /代码> 调用。

Can you explain the average value column in the DTVehicleValueRange table? Isn't this a calculated value (i.e. no need to persist it)?

It looks like you have a many-to-many relationship between Vehicle and DTValueRange, which of course would not be mapped with a join, rather with a HasManyToMany call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文