简体   繁体   English

NHibernate “多于一行”错误

[英]NHibernate "more than one row" error

I have a system where there are users and each user can have multiple tickets, and each ticket is assigned to a simulator.我有一个系统,其中有用户,每个用户可以有多张票,每张票都分配给一个模拟器。 There's a function to get all tickets of a certain status and this is returning a weird 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; }
}

The NHibernate function is as follow: 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>();
}

This is working fine, returning the tickets properly everytime, but the error happens in the next section:这工作正常,每次都正确返回票证,但错误发生在下一节:

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)
    {
    }
}

In one specific case where there are multiple tickets (10+, when the usual is having less than 5 active at a time) I'm getting the error of NHibernate.HibernateException: More than one row with the given identifier was found: 1684, for class: Entity.Simulador在有多个票证的特定情况下(10+,当通常一次少于 5 个有效票证时)我收到错误NHibernate.HibernateException: More than one row with the given identifier was found: 1684, for class: Entity.Simulador

I've checked the database and there is only one Simulator of ID 1684, plus, it's a unique key column.我检查了数据库,只有一个 ID 为 1684 的模拟器,而且它是一个唯一的键列。 The mapping of the Ticket class is the following: 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); });
}

It uses a single table (a view) because there are some records/fields from other DBs which I don't even know how it gets there.它使用单个表(一个视图),因为有一些来自其他数据库的记录/字段,我什至不知道它是如何到达那里的。

This error only happens if I try to do it "fast", if I go debugging line by line stepping into each function, the error doesn't happen.只有当我尝试“快速”执行此错误时才会发生此错误,如果我 go 逐行调试进入每个 function,则不会发生错误。 I'm guessing NHibernate is returning something wrong when it run as fast as it's supposed to be, and when I go slowly it... works?我猜 NHibernate 在运行速度达到预期速度时会返回错误,而当我 go 运行缓慢时,它...有效吗?

I've checked other questions with this message/similar messages but I still couldn't figure it.我用这条消息/类似的消息检查了其他问题,但我还是想不通。

Why and what is exactly happening and how can I solve this?为什么以及究竟发生了什么,我该如何解决这个问题?

Could it possibly be the similar namings of ID in the Ticket, User, and Simulator class? 可能是Ticket,User和Simulator类中ID的相似命名吗?

Btw, I noticed the class is called Simulator but the error said this: Entity.Simulador 顺便说一句,我注意到该类称为Simulator,但错误是这样说的:Entity.Simulador

I'm leaving this for future reference: When this error happens, it's not related to NHibernate. 我要离开这个以供将来参考:当这个错误发生,它涉及到NHibernate的。 It's in the database (as always is). 它在数据库中(一如既往)。

Should you get this error, and your classes are mapped properly, follow up the steps to get the query / query results for the commands NHibernate is using and you'll notice that there is an error in the DB structure/layout. 如果您收到此错误,并且您的类已正确映射,请执行以下步骤以获取NHibernate正在使用的命令的查询/查询结果,并且您会注意到数据库结构/布局中存在错误。 Somewhere, there is a result returning multiple of the same object with the unique ID. 在某个地方,结果将返回具有唯一ID的多个相同对象。

This could also happen when the NHibernate Session cache already contains an entity with the same Identifier and your code tries to fetch a new copy from the database into the Session当 NHibernate Session 缓存已经包含具有相同标识符的实体并且您的代码尝试从数据库中获取新副本到 Session 时,也可能发生这种情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM