加入已连接集合元素的引用属性

时间:2015-10-28 16:20:36

标签: c# hibernate join nhibernate queryover

我有 <div class="container"> <div class="row"> <asp:Repeater ID="myRepeater" runat="server" DataSourceID="myDataSourceID"> <ItemTemplate> <div class="col-sm-4"> <%# Eval("itemID")%><br /> <%# Eval("itmName")%> </div> </ItemTemplate> </asp:Repeater> </div> </div> users个表,它们具有一对一的关系(users_updateable_data)。此外,每个User.UpdateableData都可以位于User中(有一个映射的Guild; Guild.Users属性是User.GuildId的外键。)

所以我有guilds

我试图从Guild, Guild.Users, User, User.UpdateableData, User.GuildId获取满足某些条件的特定属性:

users

所以我需要做两个连接:guild + users(右外)和user + upd。数据

这就是我尝试这样做的方式:

    public class UserTournamentInfo
    {
        public virtual int Id { get; set; } // from user
        public virtual int TournamentXP { get; set; } // from upd. data
        public virtual int League { get; set; } // from user
        public virtual int TournamentClassXP { get; set; } // from upd. data
        public virtual int? GuildId { get; set; } // from user
        public virtual int? GuildLeague { get; set; } // from guild
    }

以下是我定义投影的方式:

    public virtual IEnumerable<UserTournamentInfo> GetTournamentUserToXP()
    {
        using (ISession session = SessionFactory.OpenSession())
        using (session.BeginTransaction())
        {
            User userAlias = null;
            User.UpdateableDataContainer userUpdAlias = null;
            Guild guildAlias = null;
            UserTournamentInfo dto2 = null;

            var result = session.QueryOver<Guild>(() => guildAlias)
                .JoinAlias(x => x.Users, () => userAlias, JoinType.RightOuterJoin)
                // next line doesn't compile
                .JoinAlias((User u) => u.UpdateableData, () => userUpdAlias, JoinType.RightOuterJoin)
                .Select(MakeProjections())
                .Where(() => userAlias.TournamentXP > 0)
                .TransformUsing(Transformers.AliasToBean<UserTournamentInfo>())
                .List<UserTournamentInfo>();

            session.Transaction.Commit();
            return result;
        }
    }

在我决定将 public static IProjection[] MakeProjections() { Guild guildAlias = null; User userAlias = null; User.UpdateableDataContainer userUpdAlias = null; UserTournamentInfo dto = null; return new[] { Projections.Property(() => userAlias.Id).WithAlias(() => dto.Id), Projections.Property(() => userUpdAlias.TournamentClassXP).WithAlias(() => dto.TournamentClassXP), Projections.Property(() => userUpdAlias.TournamentXP).WithAlias(() => dto.TournamentXP), Projections.Property(() => userAlias.GuildId).WithAlias(() => dto.GuildId), Projections.Property(() => userAlias.League).WithAlias(() => dto.League), Projections.Property(() => guildAlias.League).WithAlias(() => dto.GuildLeague), }; } 分成两个表格usersusers之前,它有用。

如何进行此连接查询?

1 个答案:

答案 0 :(得分:1)

我用HQL解决了它:

SELECT 
u.Id as Id, ud.TournamentClassXP as TournamentClassXP, ud.TournamentXP as TournamentXP, u.GuildId as GuildId, u.League as League, g.League as GuildLeague
FROM Guild g RIGHT OUTER JOIN g.Users u JOIN u.UpdateableData ud
WHERE ud.TournamentXP > 0