在加入的集合元素上加入引用 属性
Join referenced property on joined collection element
我有 users
和 users_updateable_data
表,它们具有一对一的关系 (User.UpdateableData
)。此外,每个 User
都可以在 Guild
中(有一个映射 Guild.Users
;User.GuildId
属性 是 guilds
的外键)。
所以我有 Guild, Guild.Users, User, User.UpdateableData, User.GuildId
.
我正在尝试从 users
获取满足某些条件的特定属性:
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
}
所以我需要做两个连接:guild + users(右外)和user + upd。数据。
我是这样尝试的:
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
和 users_updateable_data
之前它起作用了。
如何进行这个连接查询?
我用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
我有 users
和 users_updateable_data
表,它们具有一对一的关系 (User.UpdateableData
)。此外,每个 User
都可以在 Guild
中(有一个映射 Guild.Users
;User.GuildId
属性 是 guilds
的外键)。
所以我有 Guild, Guild.Users, User, User.UpdateableData, User.GuildId
.
我正在尝试从 users
获取满足某些条件的特定属性:
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
}
所以我需要做两个连接:guild + users(右外)和user + upd。数据。
我是这样尝试的:
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
和 users_updateable_data
之前它起作用了。
如何进行这个连接查询?
我用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