我想User
用他们的Characters
集合检索短版本列表(短版本也是如此)。我将 QueryOver 与投影一起使用。
public class User
{
public virtual int Id { get; set; }
public virtual string Nickname { get; set; }
public virtual ISet<Character> Characters { get; set; }
// and 30 other properties
}
public class Character
{
public virtual int Id { get; set; }
public virtual int XP { get; set; }
public virtual int UserId { get; set; }
public virtual int ClassId { get; set; }
// and 30 other properties
}
public class UserDto // short version
{
public virtual string Nickname { get; set; }
public virtual ISet<CharacterDto> Characters { get; set; }
}
public class CharacterDto // short version
{
public virtual int XP { get; set; }
public virtual int ClassId { get; set; }
}
User userAlias = null;
UserDto userDto = null;
Character characterAlias = null;
CharacterDto characterDto = null;
var result = session.QueryOver<User>(() => userAlias)
.Where(Restrictions.Like(
Projections.SqlFunction("lower", NHibernateUtil.String, Projections.Property<User>(x => x.Nickname)),
nickname.ToLowerInvariant(), MatchMode.Start))
.JoinAlias(x => x.Characters, () => characterAlias, JoinType.LeftOuterJoin)
.Select(
Projections.Property(() => userAlias.Nickname).WithAlias(() => userDto.Nickname),
Projections.Property(() => userAlias.Characters).WithAlias(() => userDto.Characters),
Projections.Property(() => characterAlias.XP).WithAlias(() => characterDto.XP),
Projections.Property(() => characterAlias.ClassId).WithAlias(() => characterDto.ClassId)
)
.TransformUsing(Transformers.AliasToBean<UserDto>())
.Take(50)
.List<UserDto>();
当我运行此代码时,它会引发异常:Could not find a setter for property ClassId in class UserDto
. 如果我删除转换器,则结果包含每个用户一个字符的条目(User1-Character1,User1-Character2,...)。每个条目都有正确的昵称和字符类 ID,但错误的 xp(而不是用户 ID)和字符集合(== null)。
如何在不使用 hql 或单独查询字符的情况下获得正确的数据?
更新
我删除了转换器并注释掉了Projections.Property(() => userAlias.Characters)...
,现在它返回了正确的结果,但是有没有办法将它们转换为像 AliasToBeanTransformer 那样的投影 dto 并折叠每个字符条目的重复用户数据?