我有 <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),
};
}
分成两个表格users
和users
之前,它有用。
如何进行此连接查询?
答案 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