VB.NET、Castle ActiveRecord 和 MySQL:唯一字段中的重复条目
当用户在唯一字段中插入重复条目时,Castle ActiveRecord 会抛出以下异常:
Castle.ActiveRecord.Framework.ActiveRecordException was unhandled
Message="Could not perform Save for SerieNotaFiscal"
Source="Castle.ActiveRecord"
StackTrace:
at Castle.ActiveRecord.ActiveRecordBase.InternalSave(Object instance, Boolean flush) at Castle.ActiveRecord.ActiveRecordBase.SaveAndFlush(Object instance) at Castle.ActiveRecord.ActiveRecordMediator.SaveAndFlush(Object instance) at bananaCo.ctrlSerieNotaFiscal.salvar(SerieNotaFiscal novoObjeto) in D:\bananaCohibernateerp\NHibernate\Control\Data\Comercial\ctrlSerieNotaFiscal.vb:line 10 at bananaCo.ctViewSerieNotaFiscal.SerieNotaFiscalSalvar() in D:\bananaCohibernateerp\NHibernate\Control\Visual\Comercial\ctViewSerieNotaFiscal.vb:line 63 at bananaCo.UISerieNotaFiscal.btnSalvar_Click(Object sender, EventArgs e) in D:\bananaCohibernateerp\NHibernate\View\Comercial\UISerieNotaFiscal.vb:line 19 at System.Windows.Forms.Control.OnClick(EventArgs e) at DevExpress.XtraEditors.BaseButton.OnClick(EventArgs e) at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at bananaCo.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
InnerException: NHibernate.Exceptions.GenericADOException
Message="could not insert: [bananaCo.SerieNotaFiscal][SQL: INSERT INTO serienf (cod_serie, dat_ult_faturamento, cdn_ult_NftNumero, id_estabelecimento, id_especie) VALUES (?, ?, ?, ?, ?)]"
Source="NHibernate"
SqlString="INSERT INTO serienf (cod_serie, dat_ult_faturamento, cdn_ult_NftNumero, id_estabelecimento, id_especie) VALUES (?, ?, ?, ?, ?)"
StackTrace:
at NHibernate.Id.Insert.AbstractSelectingDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlCommandInfo sql, Object obj, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityIdentityInsertAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) at NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event) at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj) at Castle.ActiveRecord.ActiveRecordBase.InternalSave(Object instance, Boolean flush)
InnerException: MySql.Data.MySqlClient.MySqlException
ErrorCode=-2147467259
Message="Duplicate entry '1' for key 'cod_serie'"
Number=1062
Source="MySql.Data"
StackTrace:
at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.ReadResult() at MySql.Data.MySqlClient.ResultSet.NextResult() at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) at NHibernate.Id.Insert.AbstractSelectingDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder)
InnerException:
我的 try/catch 是这样的:
Try
some code goes here ...
Catch ex As Exception
If ex.InnerException.ToString.ToLower.Contains("duplicate entry") Then
MsgBox("There is an user with this username.")
txtNome.Focus()
Else
MsgBox("Error! " & vbNewLine & ex.Message, MsgBoxStyle.Critical, "Erro")
End If
End Try
这个 catch 代码非常难看,但我不知道如何处理这个...你能帮我吗? 谢谢你们!
When user inserts a duplicate entry in a unique field, Castle ActiveRecord throws the following exception:
Castle.ActiveRecord.Framework.ActiveRecordException was unhandled
Message="Could not perform Save for SerieNotaFiscal"
Source="Castle.ActiveRecord"
StackTrace:
at Castle.ActiveRecord.ActiveRecordBase.InternalSave(Object instance, Boolean flush) at Castle.ActiveRecord.ActiveRecordBase.SaveAndFlush(Object instance) at Castle.ActiveRecord.ActiveRecordMediator.SaveAndFlush(Object instance) at bananaCo.ctrlSerieNotaFiscal.salvar(SerieNotaFiscal novoObjeto) in D:\bananaCohibernateerp\NHibernate\Control\Data\Comercial\ctrlSerieNotaFiscal.vb:line 10 at bananaCo.ctViewSerieNotaFiscal.SerieNotaFiscalSalvar() in D:\bananaCohibernateerp\NHibernate\Control\Visual\Comercial\ctViewSerieNotaFiscal.vb:line 63 at bananaCo.UISerieNotaFiscal.btnSalvar_Click(Object sender, EventArgs e) in D:\bananaCohibernateerp\NHibernate\View\Comercial\UISerieNotaFiscal.vb:line 19 at System.Windows.Forms.Control.OnClick(EventArgs e) at DevExpress.XtraEditors.BaseButton.OnClick(EventArgs e) at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at bananaCo.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
InnerException: NHibernate.Exceptions.GenericADOException
Message="could not insert: [bananaCo.SerieNotaFiscal][SQL: INSERT INTO serienf (cod_serie, dat_ult_faturamento, cdn_ult_NftNumero, id_estabelecimento, id_especie) VALUES (?, ?, ?, ?, ?)]"
Source="NHibernate"
SqlString="INSERT INTO serienf (cod_serie, dat_ult_faturamento, cdn_ult_NftNumero, id_estabelecimento, id_especie) VALUES (?, ?, ?, ?, ?)"
StackTrace:
at NHibernate.Id.Insert.AbstractSelectingDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlCommandInfo sql, Object obj, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityIdentityInsertAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) at NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event) at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj) at Castle.ActiveRecord.ActiveRecordBase.InternalSave(Object instance, Boolean flush)
InnerException: MySql.Data.MySqlClient.MySqlException
ErrorCode=-2147467259
Message="Duplicate entry '1' for key 'cod_serie'"
Number=1062
Source="MySql.Data"
StackTrace:
at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.ReadResult() at MySql.Data.MySqlClient.ResultSet.NextResult() at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) at NHibernate.Id.Insert.AbstractSelectingDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder)
InnerException:
My try/catch is something like that:
Try
some code goes here ...
Catch ex As Exception
If ex.InnerException.ToString.ToLower.Contains("duplicate entry") Then
MsgBox("There is an user with this username.")
txtNome.Focus()
Else
MsgBox("Error! " & vbNewLine & ex.Message, MsgBoxStyle.Critical, "Erro")
End If
End Try
This catch code is pretty ugly but i don't know how to handle this... Can you help me?
Thank you guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以自己检查重复条目(即在插入之前运行 SELECT,您希望将其包装在事务中)或使用 ISQLExceptionConverter 将数据库异常转换为应用程序级代码可以使用的自定义异常。
You can either check the duplicate entry yourself (i.e. run a SELECT before inserting, you'd want to wrap this in a transaction) or use ISQLExceptionConverter to convert the database exception to a custom exception that your application-level code can consume.