2015-04-22 88 views
4
//This code works fine. 
UserProfile profile = session.QueryOver<UserProfile>().Where(userProfile => userProfile.UserId == user).List().FirstOrDefault(); 

//This code throws an invalid Syntax Error 
IList<Character> characters = session.QueryOver<Character>().Where(character => character.UserId == user).List<Character>(); 

我在使用NHibernate QueryOver時遇到了上述代碼的問題。數據庫具有與用戶表的關係設置的兩個項目。它們的設置是相同的,兩個Map類都是這樣設置的。奇怪的NHibernate QueryOver語法錯誤

public class CharacterMap : ClassMap<Character> 
{ 
    public CharacterMap() 
    { 
     Id(x => x.Id).Column("id"); 
     Map(x => x.Name).Column("name"); 
     Map(x => x.Class).Column("class"); 
     Map(x => x.Level).Column("level"); 
     Map(x => x.Sex).Column("sex"); 
     Map(x => x.Stats).Column("stats"); //Varchar 2048 
     Map(x => x.Position).Column("position"); //Varchar 1024 

     References(x => x.UserId).Column("user_id"); 
     Table("character"); 
    } 
} 

public class UserProfileMap : ClassMap<UserProfile> 
{ 
    public UserProfileMap() 
    { 
     Id(x => x.Id).Column("id"); 
     Map(x => x.CharacterSlots).Column("character_slots"); 
     References(x => x.UserId).Column("user_id"); 
     Table("user_profile"); 
    } 
} 

和用戶是從該映射構建的類。

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 

最後這裏是整個會話/交易是否有幫助。即使我能夠使用相同的QueryOver語法獲取UserProfile,所有事情都會恢復正確,直到我點擊QueryOver()。

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. 
    } 
} 
+0

什麼類型的用戶ID是? – Najera

+0

用戶和用戶標識都是用戶類型。它是保存虛擬int id和虛擬int userId的類。 –

+0

你可以添加你的實體嗎? – Najera

回答

2

顯然這是由於character是mySql中的保留字。有關更多信息,請參閱this問題。

+0

非常感謝!我沒有意識到這一點,絕對不會想到這一點。我感謝幫助! –

2

低飛鵜鶘是對的。

就在反引號添加到您的映射類是這樣的:

public class CharacterMap : ClassMap<Character> 
{ 
    public CharacterMap() 
    { 
     Table("`character`"); 
    } 
}