Mysql 奇怪的NHibernate查询语法错误
问题是,当我尝试使用User类作为比较从表中获取“字符”列表时,我得到了一个语法错误,但正如您所看到的,第一个查询返回的结果很好,并且这两个结果几乎相同 有人知道为什么会这样吗?我一辈子也弄不明白 我收到的错误是:Mysql 奇怪的NHibernate查询语法错误,mysql,nhibernate,queryover,Mysql,Nhibernate,Queryover,问题是,当我尝试使用User类作为比较从表中获取“字符”列表时,我得到了一个语法错误,但正如您所看到的,第一个查询返回的结果很好,并且这两个结果几乎相同 有人知道为什么会这样吗?我一辈子也弄不明白 我收到的错误是: public class UserProfile { public virtual int Id { get; set; } public virtual User UserId { get; set; } public virtual int Charact
public class UserProfile
{
public virtual int Id { get; set; }
public virtual User UserId { get; set; }
public virtual int CharacterSlots { get; set; }
}
public class User
{
public virtual int Id { get; set; }
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual string Salt { get; set; }
public virtual string Email { get; set; }
public virtual string Algorithm { get; set; }
public virtual DateTime Created { get; set; }
public virtual DateTime Updated { get; set; }
}
public class Character
{
public virtual int Id { get; set; }
public virtual User UserId { get; set; }
public virtual string Name { get; set; }
public virtual string Class { get; set; }
public virtual string Sex { get; set; }
public virtual int? Level { get; set; }
public virtual string Stats { get; set; }
public virtual string Position { get; set; }
public virtual CharacterDetails BuildCharacterListItem()
{
return new CharacterDetails()
{
Id = Id,
Class = Class,
Name = Name,
Level = Level,
Sex = Sex
};
}
}
最后,这里是整个会话/事务(如果有帮助的话)。在我点击QueryOver()之前,一切都恢复正常,即使我能够使用相同的QueryOver语法获得UserProfile
Error: could not execute query
[ SELECT this_.id as id0_0_, this_.name as name0_0_, this_.class as
class0_0_, this_.level as level0_0_, this_.sex as sex0_0_, this_.stats
as stats0_0_, this_.position as position0_0_, this_.user_id as
user8_0_0_ FROM character this_ WHERE this_.user_id = ?p0 ]
Name:cp0 - Value:PerilousServer.Data.NHibernate.User
[SQL: SELECT this_.id as id0_0_, this_.name as name0_0_, this_.class
as class0_0_, this_.level as level0_0_, this_.sex as sex0_0_, this_.stats
as stats0_0_, this_.position as position0_0_, this_.user_id as user8_0_0_
FROM character this_ WHERE this_.user_id = ?p0]
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'character this_ WHERE this_.user_id = 11' at line 1
试试看
{
使用(var session=NHibernateHelper.OpenSession())
{
使用(var transaction=session.BeginTransaction())
{
//从登录的连接中获取用户数据,以便稍后进行验证。。
var userData=Server.Instance.ConnectedUsers[peerID];
var user=session.QueryOver().Where(u=>u.Id==userID.List().FirstOrDefault();
如果(用户!=null)
{
UserProfile profile=session.QueryOver().Where(UserProfile=>UserProfile.UserId==user.List().FirstOrDefault();
if(profile!=null)
{
//检查peerID是否已连接。
if(userData!=null&&userData.ClientData().UserId==UserId)
{
Write(“找到的用户:{0}并与字符匹配。”,profile.UserId.Id);
IList characters=session.QueryOver()。其中(character=>character.UserId==user.List();
列表字符列表=新列表();
foreach(字符中的变量字符)
{
添加(character.BuildCharacterListItem());
}
response=新消息(MessageType.response,MessageCode.Login,(字节)LoginCode.CharacterList);
AddParameter(MessageParameterCode.CharacterSlots,profile.CharacterSlots);
response.AddParameter(MessageParameterCode.CharacterList,CharacterList);
发送(应答);
Commit();
}
其他的
{
//发送消息说帐户正在尝试访问其他人的信息。
OnLog(“用户ID不匹配。PeerID不正确”);
}
}
其他的
{
//发送未找到配置文件的消息。
OnLog(“未找到用户配置文件”);
}
}
其他的
{
//发送消息说没有具有该ID的用户。NULL
OnLog(“UserID为null”);
}
}
}
}
捕获(例外e)
{
client.OnError(e.Message);
client.OnError(e.InnerException.Message);
//发送消息我们收到一个错误。
}
}
显然,这是因为字符
在mySql中是一个保留字。有关更多信息,请参阅问题。低飞鹈鹕是正确的
只需将背景标记添加到映射类中,如下所示:
try
{
using (var session = NHibernateHelper.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
//Get userData from logged in connections for verification later..
var userData = Server.Instance.ConnectedUsers[peerID];
var user = session.QueryOver<User>().Where(u => u.Id == userID).List().FirstOrDefault();
if (user != null)
{
UserProfile profile = session.QueryOver<UserProfile>().Where(userProfile => userProfile.UserId == user).List().FirstOrDefault();
if (profile != null)
{
//Check to see if peerID is actually connected.
if (userData != null && userData.ClientData<UserData>().UserId == userID )
{
Console.Write("Found user: {0} and matching to character.", profile.UserId.Id);
IList<Character> characters = session.QueryOver<Character>().Where(character => character.UserId == user).List<Character>();
List<CharacterDetails> characterList = new List<CharacterDetails>();
foreach (var character in characters)
{
characterList.Add(character.BuildCharacterListItem());
}
response = new Message(MessageType.Response, MessageCode.Login, (byte)LoginCode.CharacterList);
response.AddParameter(MessageParameterCode.CharacterSlots, profile.CharacterSlots);
response.AddParameter(MessageParameterCode.CharacterList, characterList);
client.Send(response);
transaction.Commit();
}
else
{
//Send message saying the account was trying to access someone elses information.
client.OnLog("User ID's did not match. PeerID is incorrect.");
}
}
else
{
//Send message that profile was not found.
client.OnLog("User Profile was not found.");
}
}
else
{
//Send message saying there was no user with that ID. NULL
client.OnLog("UserID was null.");
}
}
}
}
catch (Exception e)
{
client.OnError(e.Message);
client.OnError(e.InnerException.Message);
//Send message we recieved an error.
}
}
公共类CharacterMap:ClassMap
{
公共字符映射()
{
表(“`字符'”);
}
}
UserId是什么类型的?user和UserId都是user类型。它是保存虚拟int id和虚拟int userId的类。您可以添加实体吗?添加了更多信息。我感谢你的帮助。我在想可能是我的数据库出了问题,而不是查询出了问题?但在这方面,一切看起来都很好。可能是因为字符在MySql中是一个保留字,只是一个深思熟虑的感谢!我没有意识到这一点,也绝对不会想到这一点。我感谢你的帮助!
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id).Column("id");
Map(x => x.Username).Column("username");
Map(x => x.Password).Column("password");
Map(x => x.Salt).Column("salt");
Map(x => x.Email).Column("email_address");
Map(x => x.Algorithm).Column("algorithm");
Map(x => x.Created).Column("created_at");
Map(x => x.Updated).Column("updated_at");
Table("user");
}
}
public class UserProfile
{
public virtual int Id { get; set; }
public virtual User UserId { get; set; }
public virtual int CharacterSlots { get; set; }
}
public class User
{
public virtual int Id { get; set; }
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual string Salt { get; set; }
public virtual string Email { get; set; }
public virtual string Algorithm { get; set; }
public virtual DateTime Created { get; set; }
public virtual DateTime Updated { get; set; }
}
public class Character
{
public virtual int Id { get; set; }
public virtual User UserId { get; set; }
public virtual string Name { get; set; }
public virtual string Class { get; set; }
public virtual string Sex { get; set; }
public virtual int? Level { get; set; }
public virtual string Stats { get; set; }
public virtual string Position { get; set; }
public virtual CharacterDetails BuildCharacterListItem()
{
return new CharacterDetails()
{
Id = Id,
Class = Class,
Name = Name,
Level = Level,
Sex = Sex
};
}
}
Error: could not execute query
[ SELECT this_.id as id0_0_, this_.name as name0_0_, this_.class as
class0_0_, this_.level as level0_0_, this_.sex as sex0_0_, this_.stats
as stats0_0_, this_.position as position0_0_, this_.user_id as
user8_0_0_ FROM character this_ WHERE this_.user_id = ?p0 ]
Name:cp0 - Value:PerilousServer.Data.NHibernate.User
[SQL: SELECT this_.id as id0_0_, this_.name as name0_0_, this_.class
as class0_0_, this_.level as level0_0_, this_.sex as sex0_0_, this_.stats
as stats0_0_, this_.position as position0_0_, this_.user_id as user8_0_0_
FROM character this_ WHERE this_.user_id = ?p0]
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'character this_ WHERE this_.user_id = 11' at line 1
try
{
using (var session = NHibernateHelper.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
//Get userData from logged in connections for verification later..
var userData = Server.Instance.ConnectedUsers[peerID];
var user = session.QueryOver<User>().Where(u => u.Id == userID).List().FirstOrDefault();
if (user != null)
{
UserProfile profile = session.QueryOver<UserProfile>().Where(userProfile => userProfile.UserId == user).List().FirstOrDefault();
if (profile != null)
{
//Check to see if peerID is actually connected.
if (userData != null && userData.ClientData<UserData>().UserId == userID )
{
Console.Write("Found user: {0} and matching to character.", profile.UserId.Id);
IList<Character> characters = session.QueryOver<Character>().Where(character => character.UserId == user).List<Character>();
List<CharacterDetails> characterList = new List<CharacterDetails>();
foreach (var character in characters)
{
characterList.Add(character.BuildCharacterListItem());
}
response = new Message(MessageType.Response, MessageCode.Login, (byte)LoginCode.CharacterList);
response.AddParameter(MessageParameterCode.CharacterSlots, profile.CharacterSlots);
response.AddParameter(MessageParameterCode.CharacterList, characterList);
client.Send(response);
transaction.Commit();
}
else
{
//Send message saying the account was trying to access someone elses information.
client.OnLog("User ID's did not match. PeerID is incorrect.");
}
}
else
{
//Send message that profile was not found.
client.OnLog("User Profile was not found.");
}
}
else
{
//Send message saying there was no user with that ID. NULL
client.OnLog("UserID was null.");
}
}
}
}
catch (Exception e)
{
client.OnError(e.Message);
client.OnError(e.InnerException.Message);
//Send message we recieved an error.
}
}
public class CharacterMap : ClassMap<Character>
{
public CharacterMap()
{
Table("`character`");
}
}