简体   繁体   English

nhibernate查询与非相关实体的复杂连接

[英]nhibernate queryover with complex join over non-related entities

So I've spent the last few hours looking for an answer and I can't seem to find anything that makes sense. 所以我花了最后几个小时寻找答案,我似乎找不到任何有意义的东西。

public class Game
{
   public virtual Guid ID { get; set; }
   public virtual ResultStructure Structure { get; set; }
   public virtual List<Result> Results { get; set; }
}

public class Result
{
  public virtual Player Player { get; set; }
  public virtual int Position { get; set; }
}

public class ResultStructure
{
  public virtual Guid ID { get; set; }
  public virtual List<ResultOutcomes> Outcomes { get; set;}
}

public class ResultOutcomes
{
  public virtual int Position { get; set; }
  public virtual int Points { get; set; }
}

public class PlayerSummary
{
  public virtual Player Player { get; set; }
  public virtual int Points { get; set; }
}

What I'm trying to do is get a list of players and the points they've earned across a multitude of different games (there are multiple entities above game that contain lists of games). 我想要做的是获得一个玩家列表以及他们在众多不同游戏中获得的积分( game上面有多个包含game列表的实体)。 So the end result of the query would be List<PlayerSummary> The SQL I'm looking for would look something like this: 所以查询的最终结果将是List<PlayerSummary>我正在寻找的SQL看起来像这样:

SELECT p.*, Sum(rs.points) FROM result r
  JOIN player p on r.playerid = p.id
  JOIN game g on r.gameid = g.id
  JOIN resultstructure rs on g.resultstructureid = rs.id
  JOIN resultoutcomes ro on rs.id = ro.resultstructureid AND ro.position = r.position

Note, I will also need to do some querying/summing against the structure entity which is why it's included. 注意,我还需要对结构实体进行一些查询/求和,这就是它包含的原因。

I'm trying to do this with NHibernate, using the TypeSafe stuff, and my plan is for the application to be database agnostic, so I can't use direct SQL (currently it's using Postgres, but I may move to SQL server at some point). 我正在尝试使用NHibernate,使用TypeSafe的东西,我的计划是让应用程序与数据库无关,所以我不能使用直接SQL(目前它使用Postgres,但我可能会转移到SQL服务器点)。

I don't particularly want to use the "HQL" stuff where you use those magic strings, so I'm trying to use Linq or QueryOver/Query. 我并不特别想使用那些使用这些魔术字符串的“HQL”内容,因此我尝试使用Linq或QueryOver / Query。

Can anyone point me in the right direction? 谁能指出我正确的方向?

It seems that the above is possible in my situation as there is a relationship, it's just not direct. 看来上述情况在我的情况下是可能的,因为存在关系,它只是不直接。

You can use JoinAlias . 您可以使用JoinAlias

the basic difference is that using JoinAlias , you can join multiple tables to same base table, where as with JoinQueryOver it takes a linear progression through the tables joining each to the previous table only. 基本的区别在于使用JoinAlias ,您可以将多个表连接到相同的基表,与JoinQueryOver一样, JoinQueryOver需要通过表连接每个表到上一个表的线性进展。

so the query looks like this. 所以查询看起来像这样。

Result resultAlias = null;
ResultOutcome outcomeAlias = null;
ResultStructure structureAlias = null;

var results = Session.QueryOver(() => resultAlias) // Assigns resultAlias so it can be used further in the query.
   .Inner.JoinQueryOver(x => x.Game) // returns a QueryOver Game so you can do a where on the game object, or join further up the chain.
   .Inner.JoinAlias(x => x.ResultStructure, () => structureAlias) // joins on the Structure table but returns the QueryOver for the Game, not the structure.
   .Inner.JoinAlias(() => structureAlias.Outcomes, () => outcomeAlias) // same again for the outcomes
   .Where(() => resultAlias.Position == outcomeAlias.Position)
   .Select(
        Projections.Group(() => resultAlias.Player),
        Projections.Sum(() => outcomeAlias.Points)
   );

That should give people the idea. 这应该给人们这个想法。 The downside to this is that the restriction on "Position" doesn't happen on the Join, but rather in the Where clause. 这样做的缺点是对“位置”的限制不会发生在Join上,而是发生在Where子句中。 I'm happy to hear from anyone who has an option for doing that as this would force the database query planner down a specific route. 我很高兴听到有人可以选择这样做,因为这会强制数据库查询计划程序沿着特定的路径行进。

Still working on the transformations and the ordering, but that's got me a lot further. 仍然致力于转型和订购,但这让我更进一步。

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

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