简体   繁体   中英

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.

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:

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

I've checked the database and there is only one Simulator of ID 1684, plus, it's a unique key column. The mapping of the Ticket class is the following:

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. 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?

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?

Btw, I noticed the class is called Simulator but the error said this: Entity.Simulador

I'm leaving this for future reference: When this error happens, it's not related to 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. Somewhere, there is a result returning multiple of the same object with the unique 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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