我可能映射错了,或者其他什么,NHibernate 似乎让我感到困惑,但是当它工作时它很顺利。到目前为止,我已经搜索了所有内容,并且仍在学习 NHibernate。我正在尝试查询具有 accountId 的地址,该地址将链接到我的 AccountUser 地图,该地图具有将链接到我的 Usermap 的 userId。我想查找用户名为“joe”的所有地址。有人知道我会如何处理我的查询吗?下面是我的地图和查询:
public AddressMap()
{
Table("Address");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.FirstName);
Map(x => x.MiddleName);
Map(x => x.LastName);
Map(x => x.CompanyName);
Map(x => x.Street);
Map(x => x.City);
Map(x => x.State);
Map(x => x.AccountID);
Map(x => x.Name);
References(x => x.AccountUser);
}
public class AccountUserMap: ClassMap<AccountUser>
{
public AccountUserMap()
{
Table("AccountUser");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.UserId);
Map(x => x.AccountID).Not.Nullable();
References(x => x.User);
}
}
public UserMap()
{
Table("User");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Name);
}
// non working query:
Address address = null;
User user = null;
AccountUser accountUser = null;
var query = session.QueryOver<Address>(() => address)
.JoinQueryOver(x => x.AccountUser, () => accountUser)
.Where(() => accountUser.AccountID == address.AccountID)
.JoinQueryOver(y => y.User, () => user)
.Where(() => user.Id == accountUser.UserId )
.And(() => user.Name == "joe")
.List()).ToList();
错误消息也不是很有帮助...
{"无效的列名 'AccountUser_id'。\r\n无效的列名 'User_id'。\r\n无效的列名 'AccountUser_id'。\r\n无效的列名 'User_id'。"}
没有用下划线命名的东西,所以它必须是 NHibernate 的东西
此外,下面的这个 SQL 查询有效,并且是我想要做的:
SELECT TOP 1000
[AddressID]
,[FirstName]
,[MiddleName]
,[LastName]
FROM Address
inner join AccountUser accountUser
on AccountUser.AccountID = Address.AccountID
inner join User user
on user.UserID = accountUser.UserID
where user.Name= 'joe'
希望那是足够的细节