nHibernate:<some type =“”>实例的标识符从X更改为X </some>

时间:2014-11-22 05:13:56

标签: nhibernate asp.net-web-api

当我为此发帖子时 端点,我得到错误:&#34; PupilEntity实例的标识符从100021823改为100021823。&#34;为什么?我相信下面的代码只能读一读 有没有看到的写作?我正在使用web api和nHibernate。也许它根本不是学生,而是如何解决问题 我们设置事务然后尝试提交/回滚。请注意,事务是在ActionFilterAttribute中设置和完成的。但是,我是.net堆栈的新手,所以不确定它是什么。

public class HibernateSessionFilter : ActionFilterAttribute
{

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        //Debug.WriteLine("Opening Hibernate Session.");
        CurrentSessionContext.Bind(HibernateConfig.SessionFactory.OpenSession());

        //Only apply transaction for post or put requests
        string methodType = actionContext.Request.Method.Method;

        if (methodType.ToUpper().Equals(HttpVerbs.Post.ToString().ToUpper())
            || methodType.ToUpper().Equals(HttpVerbs.Put.ToString().ToUpper()))
        {
            if (HibernateConfig.SessionFactory.GetCurrentSession() != null)
                HibernateConfig.SessionFactory.GetCurrentSession().BeginTransaction();
        }
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        ISession session = CurrentSessionContext.Unbind(HibernateConfig.SessionFactory);
        try
        {
            if (session.Transaction.IsActive)
                if (actionExecutedContext.Exception != null)
                    session.Transaction.Rollback();
                else
                    session.Transaction.Commit();
        }
        finally
        {
            session.Transaction.Dispose();
            //session.Flush();
            session.Close();
            session.Dispose();
        }
        //Debug.WriteLine("Closed Hibernate Session.");
    }
}

    //Marks a pupil as withdrawn
    [ActionName("WithdrawPupil")]
    [HttpPost]
    public WithdrawPupilResponse WithdrawPupil(int id, [FromBody] WithdrawPupilRequest reqData)
    {
        int code = reqData.code;

        try
        {
            HibernateConfig.SessionFactory.GetCurrentSession().Get<PupilEntity>(id);
            return new WithdrawPupilResponse(true, PUPIL_WITHDRAWN_MSG);
        }
        catch (InvalidValueException ex)
        {
            string message = String.Format(WITHDRAW_PUPIL_ERROR_MSG, id, ex.Message);
            throw new ClientsFaultException(message, NewMsmiErrorCodes.WITHDRAW_PUPIL_ERROR, ex);
        }
        catch (Exception ex)
        {
            throw new ServersFaultException(string.Format("Unexpected Problem withdrawing pupil {0}.", id), 1234L, ex);
        }
    }
}


public class WithdrawPupilResponse
{
    private Boolean success { get; set; }
    private string message { get; set; }

    public WithdrawPupilResponse(Boolean success, string message)
    {
        this.success = success;
        this.message = message;
    }
}    

以下是来自hibernate的日志:

2014-11-22 00:25:29,888 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Impl.SessionImpl [session-id=a0d4aaf3-bbfa-4c2c-a2e2-ae6c0db424ba] opened session at timestamp: 635522127298, for session factory: [/2d25c8509fe643b18a8526e5bd764d33]
2014-11-22 00:25:29,893 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Transaction.AdoTransaction Begin (Unspecified)
2014-11-22 00:25:29,894 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Connection.DriverConnectionProvider Obtaining IDbConnection from Driver
2014-11-22 00:25:29,904 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.DefaultLoadEventListener loading entity: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
2014-11-22 00:25:29,905 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.DefaultLoadEventListener attempting to resolve: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
2014-11-22 00:25:29,907 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.DefaultLoadEventListener object not resolved in any cache: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
2014-11-22 00:25:29,908 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister Fetching entity: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
2014-11-22 00:25:29,909 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader loading entity: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
2014-11-22 00:25:29,928 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher Opened new IDbCommand, open IDbCommands: 1
2014-11-22 00:25:29,929 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher Building an IDbCommand object for the SqlString: SELECT pupilentit0_.PupilID as PupilID2_0_, pupilentit0_.CurrentMonthStatus as CurrentM2_2_0_, pupilentit0_.Status as Status2_0_, pupilentit0_.LastUpdated as LastUpda4_2_0_, pupilentit0_.Active as Active2_0_ FROM Foo.dbo.Pupils pupilentit0_ WHERE pupilentit0_.PupilID=?
2014-11-22 00:25:29,930 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Type.Int32Type binding '100021823' to parameter: 0
2014-11-22 00:25:29,932 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] INFO  NHibernate.Loader.Loader SELECT pupilentit0_.PupilID as PupilID2_0_, pupilentit0_.CurrentMonthStatus as CurrentM2_2_0_, pupilentit0_.Status as Status2_0_, pupilentit0_.LastUpdated as LastUpda4_2_0_, pupilentit0_.Active as Active2_0_ FROM Foo.dbo.Pupils pupilentit0_ WHERE pupilentit0_.PupilID=@p0
2014-11-22 00:25:29,937 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.SQL
    SELECT
        pupilentit0_.PupilID as PupilID2_0_,
        pupilentit0_.CurrentMonthStatus as CurrentM2_2_0_,
        pupilentit0_.Status as Status2_0_,
        pupilentit0_.LastUpdated as LastUpda4_2_0_,
        pupilentit0_.Active as Active2_0_
    FROM
        Foo.dbo.Pupils pupilentit0_
    WHERE
        pupilentit0_.PupilID=@p0;
    @p0 = 100021823 [Type: Int32 (0)]
2014-11-22 00:25:29,938 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Transaction.AdoTransaction Enlist Command
2014-11-22 00:25:29,942 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher ExecuteReader took 3 ms
2014-11-22 00:25:29,942 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher Opened IDataReader, open IDataReaders: 1
2014-11-22 00:25:29,943 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader processing result set
2014-11-22 00:25:29,944 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader result set row: 0
2014-11-22 00:25:29,945 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader result row: EntityKey[Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
2014-11-22 00:25:29,948 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader Initializing object from DataReader: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
2014-11-22 00:25:29,951 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister Hydrating entity: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
2014-11-22 00:25:29,952 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Type.StringType returning '-' as column: CurrentM2_2_0_
2014-11-22 00:25:29,953 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Type.StringType returning 'Bill overdue or extension exhausted' as column: Status2_0_
2014-11-22 00:25:29,953 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Type.DateTimeType returning '1/16/2014 10:44:22 AM' as column: LastUpda4_2_0_
2014-11-22 00:25:29,954 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Type.BooleanType returning 'True' as column: Active2_0_
2014-11-22 00:25:29,955 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader done processing result set (1 rows)
2014-11-22 00:25:29,955 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Driver.NHybridDataReader running NHybridDataReader.Dispose()
2014-11-22 00:25:29,956 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher Closed IDataReader, open IDataReaders :0
2014-11-22 00:25:29,956 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher DataReader was closed after 17 ms
2014-11-22 00:25:29,957 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher Closed IDbCommand, open IDbCommands: 0
2014-11-22 00:25:29,958 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader total objects hydrated: 1
2014-11-22 00:25:29,959 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Engine.TwoPhaseLoad resolving associations for [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
2014-11-22 00:25:29,962 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Engine.Loading.LoadContexts creating collection wrapper:[Foo.NewMSMI.Models.Entities.PupilEntity.PupilWithdrawal#100021823]
2014-11-22 00:25:29,967 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Engine.TwoPhaseLoad done materializing entity [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
2014-11-22 00:25:29,967 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Engine.StatefulPersistenceContext initializing non-lazy collections
2014-11-22 00:25:29,968 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader done entity load
2014-11-22 00:25:30,015 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Transaction.AdoTransaction Start Commit
2014-11-22 00:25:30,016 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.AbstractFlushingEventListener flushing session
2014-11-22 00:25:30,017 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.AbstractFlushingEventListener processing flush-time cascades
2014-11-22 00:25:30,019 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] INFO  NHibernate.Engine.Cascade processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Foo.NewMSMI.Models.Entities.PupilEntity
2014-11-22 00:25:30,021 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] INFO  NHibernate.Engine.Cascade cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: Foo.NewMSMI.Models.Entities.PupilEntity.PupilWithdrawal
2014-11-22 00:25:30,022 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] INFO  NHibernate.Engine.Cascade done cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: Foo.NewMSMI.Models.Entities.PupilEntity.PupilWithdrawal
2014-11-22 00:25:30,022 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] INFO  NHibernate.Engine.Cascade done processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Foo.NewMSMI.Models.Entities.PupilEntity
2014-11-22 00:25:30,023 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.AbstractFlushingEventListener dirty checking collections
2014-11-22 00:25:30,024 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.AbstractFlushingEventListener Flushing entities and processing referenced collections
2014-11-22 00:25:33,005 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Transaction.AdoTransaction IDbTransaction disposed.
2014-11-22 00:25:33,005 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Impl.SessionImpl transaction completion
2014-11-22 00:25:33,006 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.ConnectionManager aggressively releasing database connection
2014-11-22 00:25:33,006 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Connection.ConnectionProvider Closing connection
2014-11-22 00:25:33,008 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Impl.SessionImpl closing session
2014-11-22 00:25:33,008 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher running BatcherImpl.Dispose(true)
2014-11-22 00:25:33,011 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Impl.SessionImpl [session-id=a0d4aaf3-bbfa-4c2c-a2e2-ae6c0db424ba] running ISession.Dispose()
2014-11-22 00:25:33,011 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Impl.SessionImpl [session-id=a0d4aaf3-bbfa-4c2c-a2e2-ae6c0db424ba] executing real Dispose(True)
NHibernate.HibernateException: identifier of an instance of Foo.NewMSMI.Models.Entities.PupilEntity was altered from 100021823 to 100021823
   at NHibernate.Event.Default.DefaultFlushEntityEventListener.CheckId(Object obj, IEntityPersister persister, Object id, EntityMode entityMode)
   at NHibernate.Event.Default.DefaultFlushEntityEventListener.GetValues(Object entity, EntityEntry entry, EntityMode entityMode, Boolean mightBeDirty, ISessionImplementor session)
   at NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent event)
   at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(FlushEvent event)
   at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event)
   at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
   at NHibernate.Impl.SessionImpl.Flush()
   at NHibernate.Transaction.AdoTransaction.Commit()
   at Foo.NewMSMI.Filters.HibernateSessionFilter.OnActionExecuted(HttpActionExecutedContext actionExecutedContext) in C:\Users\thebiglebowski\source\Foo.NET\dotnet\Foo.NewMSMI\Filters\HibernateSessionFilter.cs:line 43

1 个答案:

答案 0 :(得分:0)

问题是从int转换为long。我有一个实体,其“int”字段映射到sql server数据库中的“bigint”。我有一个DTO对象来给出响应,但这有一个很长的字段。 Hibernate不喜欢long和int之间的不匹配。当我把所有字段都变成“int”时,问题就消失了。