简体   繁体   English

流利的nHibernate连接ID和null值

[英]Fluent nHibernate join id and null values

I think that I have now searched the Internet in all possible ways, and I didn't find the answer, or maybe just didn't understand the answer good enough to implement it into my current project. 我认为我现在已经以各种可能的方式搜索了Internet,但找不到答案,或者可能只是对答案的理解不足,无法将其实施到当前项目中。 So I'm very happy with a solution here. 因此,我对这里的解决方案感到非常满意。

It's about planning, and as illustrated I have a machine, that run any hour during the week. 这与计划有关,并且如图所示,我有一台机器,该机器在一周的任何时间运行。 But, in a "Nonproduction" table, I have timespans (estimatedStart - estimatedStop), where production is not possible. 但是,在“非生产”表中,我有一个时间跨度(estimatedStart -estimatedStop),在这里不可能进行生产。 A nonProduction record, can be for a specific machine - but it can also be for the company. 非生产记录可以用于特定机器-但也可以用于公司。 In that case the machineID will be null. 在这种情况下,machineID将为null。

SO, what I want is to select a machine with all it's nonProduction records AND the nonProduction records where machineId is null. 因此,我要选择一台具有所有nonProduction记录和machineId为null的nonProduction记录的机器。

来自MySQL的图

The SQL statement would be VERY easy! SQL语句非常简单!

select * from machine m
left outer join nonproduction np on (m.machine_id = np.machineID or np.machineID is null)
where m.machine_id=119;

上述SQL的选择结果

My fluent Machine mapping goes here: (Some code has been removed for clarity) 我的流利机器映射在这里:(为清晰起见,一些代码已删除)

public class MachineMap : ClassMap<Machine>
    {
        public MachineMap()
        {
            Table("machine");
            Id(x => x.MachineId, "machine_id").GeneratedBy.Identity();
            Map(x => x.Name, "name");
            Map(x => x.Number, "machinenumber");
            Map(x => x.Size, "size");
            Map(x => x.Data1, "data1");
            Map(x => x.Data2, "data2");
            Map(x => x.Data3, "data3");

            HasMany(x => x.NonProductions)
                .KeyColumn("machineID").KeyNullable()
                .AsBag();

        }
    }

public class NonProductionMap : ClassMap<NonProduction>
        {
            public NonProductionMap()
            {
                Table("nonproduction");
                Id(x => x.NonproductionId, "Nonproduction_id").GeneratedBy.Identity();
                Map(x => x.NonproductionTypeId, "nonproduction_typeID");
                Map(x => x.MachineId, "machineID").Nullable();
                Map(x => x.WorkerId, "workerID");
                Map(x => x.EstimatedStart, "estimated_start");
                Map(x => x.EstimatedStop, "estimated_stop");
                Map(x => x.Visible, "nonproductionvisible");
                Map(x => x.Repetitiontime, "repetitiontime");

                References(x => x.Machine)
                    .Column("machineID")
                    .Not.Insert()
                    .Not.Update();
            }
        }

Repository code here: 此处的存储库代码:

public IEnumerable<Machine> GetMachinesForCalendar(int[] ids = null)
    {
        Machine m = null;
        Order o = null;
        NonProduction n = null;

        var query = Session.QueryOver(() => m)
            .JoinAlias(() => m.Orders, () => o, JoinType.LeftOuterJoin)
            .JoinAlias(() => m.NonProductions, () => n, JoinType.LeftOuterJoin, Restrictions.Where(() => n.MachineId == m.MachineId || n.MachineId == null));

        if (ids != null && ids.Any())
        {
            query = query
                .WhereRestrictionOn(() => m.MachineId)
                .IsIn(ids);
        }

        return query
            .List()
            .Distinct()
            .ToList();

    }

I know that the "n.MachineId == m.MachineId" in the restriction part is implied, but as I started to write, I really can't find a nice solution here. 我知道限制部分暗含了“ n.MachineId == m.MachineId”,但是当我开始写时,我真的在这里找不到很好的解决方案。

I have to mention, that the database is very old, and contains a lot of data, so it is unfortunately not possible to re-design it. 我不得不提到,该数据库非常老,并且包含很多数据,因此很遗憾无法重新设计它。 :( :(

I do not believe this to be doable by mappings. 我不认为通过映射是可行的。

I would map in the one-to-many only the machine bound nonProduction entities. 我会在一对多中仅映射机器绑定的nonProduction实体。

The general ones (without a machine id) would be loaded by a dedicated query, and you would merge the two lists for your processing. 通用查询(没有计算机ID)将通过专用查询加载,您将合并两个列表以进行处理。

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

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