NHibernate could not get or update next Value[sql:] Object name ' Hibernate_unique_key ' is not valid.

Source: Internet
Author: User
Tags stack trace

Error message:

------------------------------------------------------NHibernate.Exceptions.GenericADOException:could not Get orUpdateNextValue[sql:]---> System.Data.SqlClient.SqlException: Object name' Hibernate_unique_key ' is invalid. In System.Data.SqlClient.SqlConnection.OnError (SqlException exception,BooleanBreakconnection, Action '1wrapcloseinaction) in System.Data.SqlClient.SqlInternalConnection.OnError (SqlException exception,BooleanBreakconnection, Action '1wrapcloseinaction) in System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning (Tdsparserstateobject stateobj,BooleanCallerhasconnectionlock,BooleanAsyncclose) in System.Data.SqlClient.TdsParser.TryRun (Runbehavior runbehavior, SqlCommand Cmdhandler, SqlDataReader DataStream, Bulkcopysimpleresultset Bulkcopyhandler, Tdsparserstateobject stateobj,Boolean& Dataready) in System.Data.SqlClient.SqlDataReader.TryConsumeMetaData () in SYSTEM.DATA.SQLCLIENT.SQLDATAREADER.G Et_metadata () in System.Data.SqlClient.SqlCommand.FinishExecuteReader (SqlDataReader ds, Runbehavior Runbehavior,Stringresetoptionsstring) in System.Data.SqlClient.SqlCommand.RunExecuteReaderTds (CommandBehavior Cmdbehavior, Runbehavior Runbehavior,BooleanReturnstream,BooleanAsync, Int32 timeout, task& Task,BooleanAsyncWrite, SqlDataReader DS,BooleanDescribeparameterencryptionrequest) in System.Data.SqlClient.SqlCommand.RunExecuteReader (CommandBehavior Cmdbehavior, Runbehavior Runbehavior,BooleanReturnstream,Stringmethod, TaskCompletionSource '1Completion, Int32 timeout, task& Task,BooleanAsyncWrite) in System.Data.SqlClient.SqlCommand.RunExecuteReader (CommandBehavior Cmdbehavior, Runbehavior Runbehavior,BooleanReturnstream,Stringmethod) in System.Data.SqlClient.SqlCommand.ExecuteReader (CommandBehavior behavior,Stringmethod) in System.Data.SqlClient.SqlCommand.ExecuteDbDataReader (CommandBehavior behavior) in System.Data.Common.DbCom Mand. System.Data.IDbCommand.ExecuteReader () in NHibernate.Id.TableGenerator.DoWorkInCurrentTransaction ( Isessionimplementor session, IDbConnection Conn, idbtransaction transaction) in NHibernate.Engine.TransactionHelper.Work.DoWork (idbconnection connection, idbtransaction transaction)--- The end of the internal exception stack trace---in NHibernate.Engine.TransactionHelper.Work.DoWork (idbconnection connection, idbtransaction Transaction) in NHibernate.Transaction.AdoNetTransactionFactory.ExecuteWorkInIsolation (Isessionimplementor session , iisolatedwork work,Booleantransacted) in NHibernate.Transaction.AdoNetWithDistributedTransactionFactory.ExecuteWorkInIsolation ( Isessionimplementor session, Iisolatedwork work,Booleantransacted) in NHibernate.Engine.Transaction.Isolater.DoIsolatedWork (iisolatedwork work, Isessionimplementor session ) in NHibernate.Engine.TransactionHelper.DoWorkInNewTransaction (Isessionimplementor session) in NHibernate.Id.TableGe Nerator. Generate (Isessionimplementor session,Objectobj) at NHibernate.Id.TableHiLoGenerator.Generate (Isessionimplementor session,Objectobj) in NHibernate.Event.Default. Abstractsaveeventlistener.savewithgeneratedid (ObjectEntityStringEntityName,ObjectAnything, IEventSource source,Booleanrequiresimmediateidaccess) in NHibernate.Event.Default. Defaultsaveorupdateeventlistener.savewithgeneratedorrequestedid (saveorupdateeventEvent) in NHibernate.Event.Default. Defaultsaveeventlistener.savewithgeneratedorrequestedid (saveorupdateeventEvent) in NHibernate.Event.Default. Defaultsaveorupdateeventlistener.entityistransient (saveorupdateeventEvent) in NHibernate.Event.Default. Defaultsaveeventlistener.performsaveorupdate (saveorupdateeventEvent) in NHibernate.Event.Default. Defaultsaveorupdateeventlistener.onsaveorupdate (saveorupdateeventEvent) in NHibernate.Impl.SessionImpl.FireSave (saveorupdateeventEvent) in NHibernate.Impl.SessionImpl.Save (Objectobj) in WindowsFormsApplication1.Form1.Form1_Load (Objectsender, EventArgs e) Location C:\Users\JianKunKing\Desktop\NHibernate\NHibernate03\WindowsFormsApplication1\Form1.cs: line number A---------------------------Determine---------------------------

Workaround:
I ' ve genereated a schema for my (SQL 2005) DB using Schemaexport, and it ' s created a table

CREATE TABLE [dbo].[hibernate_unique_key](
 [next_hi][int] NULL
) ON [PRIMARY]

When I try to add an entity, I get the error:

---------------------------
---------------------------NHibernate.Id.IdentifierGenerationException:Could not read a hi value-you need to populate the Table:hibernate_unique_key in NHibernate. Id. Tablegenerator. Doworkincurrenttransaction(Isessionimplementor session, IDbConnection Conn, idbtransaction transaction) in NHibernate. Engine. Transactionhelper. work. DoWork(idbconnection connection, idbtransaction transaction) in NHibernate. Transaction. Adonettransactionfactory. Executeworkinisolation(Isessionimplementor session, iisolatedwork work, Boolean transacted) in NHibernate. Transaction. Adonetwithdistributedtransactionfactory. Executeworkinisolation(Isessionimplementor session, iisolatedwork work, Boolean transacted) in NHibernate. Engine. Transaction. Isolater. Doisolatedwork(iisolatedwork work, Isessionimplementor session) in NHibernate. Engine. Transactionhelper. Doworkinnewtransaction(Isessionimplementor session) in NHibernate. Id. Tablegenerator. Generate(isessionimplementor session, Object obj) in NHibernate. Id. Tablehilogenerator. Generate(isessionimplementor session, Object obj) in NHibernate. Event. Default. Abstractsaveeventlistener. Savewithgeneratedid(Object entity, String entityname, Object anything, IEventSource source, Boolean requiresimmediateidaccess) in Nhibernat E. Event. Default. Defaultsaveorupdateeventlistener. Savewithgeneratedorrequestedid(Saveorupdateevent event) in NHibernate. Event. Default. Defaultsaveeventlistener. Savewithgeneratedorrequestedid(Saveorupdateevent event) in NHibernate. Event. Default. Defaultsaveorupdateeventlistener. Entityistransient(Saveorupdateevent event) in NHibernate. Event. Default. Defaultsaveeventlistener. Performsaveorupdate(Saveorupdateevent event) in NHibernate. Event. Default. Defaultsaveorupdateeventlistener. Onsaveorupdate(Saveorupdateevent event) in NHibernate. Impl. Sessionimpl. Firesave(Saveorupdateevent event) in NHibernate. Impl. Sessionimpl. Save(Object obj) in WindowsFormsApplication1. Form1. Form1_load (Object sender, EventArgs e) Location C:\Users\JianKunKing\Desktop\NHibernate\NHibernate03\WindowsFormsApplication1 \form1. CS: line number the---------------------------Determine---------------------------

Solution:
NHibernate expects to find a value this stores the current HI value in this table, ie it first runs something like:

current_hi =[SELECT max(next_hi) FROM hibernate_unique_key].

So all your need to does is seed this table with an initial number, ie:

INSERT INTO hibernate_unique_key(next_hi) VALUES (0)

Reference article:
Exception occurs when you deploy Olat to myeclipse with the correct database configuration: Error with Hilo in NHibernate-"could not read a hi value-you

NHibernate could not get or update next Value[sql:] Object name ' Hibernate_unique_key ' is not valid.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.