簡體   English   中英

NHibernate “多於一行”錯誤

[英]NHibernate "more than one row" error

我有一個系統,其中有用戶,每個用戶可以有多張票,每張票都分配給一個模擬器。 有一個 function 可以獲取特定狀態的所有票證,這會返回一個奇怪的錯誤。

public class User 
{
    public int ID { get; set; }
}

public class Simulator
{
    public int ID { get; set; }
}

public class Ticket
{
    public int ID { get; set; }
    public User User { get; set; }
    public Simulator Simulator { get; set; }
}

NHibernate function如下:

public IList<Ticket> GetActiveTicketsPerUser(int userID)
{
    var criteria = Repository.Session.GetSession().CreateCriteria("GetTickets", "Ticket");

    criteria.Add(Restrictions.Eq("Ticket.User.ID", userID));
    criteria.Add(Restrictions.Eq("Ticket.Status.ID", new List<int>() { 1, 2 }); //Status the active tickets

    return criteria.List<Ticket>();
}

這工作正常,每次都正確返回票證,但錯誤發生在下一節:

public HttpResponseMessage GetActiveTickets()
{
    try
    {
        int userID = Convert.ToInt32(HttpContext.Current.Items["UserID"]);

        IList<Ticket> ticketList = Service.GetTickets(userID);

        //Converting to another type specific to return in the HttpResponseMessage
        IList<TicketResponse> rTickets = new List<TicketResponse>();
        for(int i = 0; i < ticketList.Count; i++)
        {
            TicketResponse ticket = TResponse.Convert(ticketList[i]);
            rTickets.Add(ticket);
        }

        return Request.CreateResponse(HttpStatusCode.OK, rTickets);
    }
    catch (Exception ex)
    {
    }
}

在有多個票證的特定情況下(10+,當通常一次少於 5 個有效票證時)我收到錯誤NHibernate.HibernateException: More than one row with the given identifier was found: 1684, for class: Entity.Simulador

我檢查了數據庫,只有一個 ID 為 1684 的模擬器,而且它是一個唯一的鍵列。 Ticket class 的映射如下:

public class TicketMap : ClassMapping<Ticket>
{
    Lazy(true);
    ID(x => x.ID, map => { map.Column("TicketID"); map.Generator(Generators.Identity); });
    ManyToOne(x => x.Simulator, map => { map.Column("SimulatorID"); map.Cascade(Cascade.None); });
}

它使用單個表(一個視圖),因為有一些來自其他數據庫的記錄/字段,我什至不知道它是如何到達那里的。

只有當我嘗試“快速”執行此錯誤時才會發生此錯誤,如果我 go 逐行調試進入每個 function,則不會發生錯誤。 我猜 NHibernate 在運行速度達到預期速度時會返回錯誤,而當我 go 運行緩慢時,它...有效嗎?

我用這條消息/類似的消息檢查了其他問題,但我還是想不通。

為什么以及究竟發生了什么,我該如何解決這個問題?

可能是Ticket,User和Simulator類中ID的相似命名嗎?

順便說一句,我注意到該類稱為Simulator,但錯誤是這樣說的:Entity.Simulador

我要離開這個以供將來參考:當這個錯誤發生,它涉及到NHibernate的。 它在數據庫中(一如既往)。

如果您收到此錯誤,並且您的類已正確映射,請執行以下步驟以獲取NHibernate正在使用的命令的查詢/查詢結果,並且您會注意到數據庫結構/布局中存在錯誤。 在某個地方,結果將返回具有唯一ID的多個相同對象。

當 NHibernate Session 緩存已經包含具有相同標識符的實體並且您的代碼嘗試從數據庫中獲取新副本到 Session 時,也可能發生這種情況

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM