我有两个连接的子类从同一个表中读取作为鉴别器的可空字段。我已经能够使用如下的子选择读取这两个实体:
<joined-subclass name="EntityA"
table="t_entity"
subselect="SELECT * FROM t_entity WHERE t_entity.discriminator is not null">
<key column="t_uid"></key>
<!-- more mapping -->
</joined-subclass>
<joined-subclass name="EntityB"
table="t_entity"
subselect="SELECT * FROM t_entity WHERE t_entity.discriminator is null">
<key column="t_uid"></key>
<!-- more mapping -->
</joined-subclass>
一切都很好但是当我尝试删除两个实体中的一个时,我收到语法错误:
NHibernate.Exceptions.GenericADOException was unhandled
HResult=-2146232832
Message=could not delete: [EntityA#27]
[SQL: DELETE FROM ( SELECT *
FROM t_entity
WHERE t_entity.discriminator is not null ) WHERE t_uid = ?]
Source=NHibernate
有关如何通过可空字段区分同一个表中的实体的更好的想法吗?
这里的堆栈跟踪:
at NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Int32 j, Object obj, SqlCommandInfo sql, ISessionImplementor session, Object[] loadedState)
at NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Object obj, ISessionImplementor session)
at NHibernate.Action.EntityDeleteAction.Execute()
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
at NHibernate.Engine.ActionQueue.ExecuteActions()
at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
at Nephila.Toolkit.Data.Implementations.Transaction.Commit() in C:\Projects\Git\nephilaapiTFS\Toolkit\Nephila.Toolkit.Data.Implementations\Transaction.cs:line 48
at Nephila.Dashboard.ServiceLayer.WidgetRegistrationService.UnregisterWidget(Widget widget) in C:\Projects\Git\nephilaapiTFS\Dashboard\Nephila.Dashboard.ServiceLayer\WidgetRegistrationService.cs:line 206
at Nephila.Dashboard.WidgetRegistrationConsole.Instance.UnregisterWidget(String widgetUrlName) in C:\Projects\Git\nephilaapiTFS\Nephila.Dashboard.WidgetRegistrationConsole\Program.cs:line 254
at Nephila.Dashboard.WidgetRegistrationConsole.Instance.Display(String[] args) in C:\Projects\Git\nephilaapiTFS\Nephila.Dashboard.WidgetRegistrationConsole\Program.cs:line 97
at Nephila.Dashboard.WidgetRegistrationConsole.Program.Main(String[] args) in C:\Projects\Git\nephilaapiTFS\Nephila.Dashboard.WidgetRegistrationConsole\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Data.SqlClient.SqlException
HResult=-2146232060
Message=Incorrect syntax near '('.
Incorrect syntax near the keyword 'WHERE'.
Statement(s) could not be prepared.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=15
LineNumber=1
Number=102
Procedure=""
Server=(local)
State=1
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
at NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Int32 j, Object obj, SqlCommandInfo sql, ISessionImplementor session, Object[] loadedState)
答案 0 :(得分:1)
您可以使用基于鉴别器的继承映射,而不是使用子选择,使用鉴别器的公式,即
<class name="BaseEntity" table="t_entity">
<!-- base id and properties mapping -->
<discriminator formula="CASE discriminator is not null
WHEN true THEN 'EntityA'
ELSE 'EntityB'
END" />
<subclass name="EntityA" discriminator-value="EntityA">
<!-- more mapping -->
</subclass>
<subclass name="EntityB" discriminator-value="EntityB">
<!-- more mapping -->
</subclass>
</class>
然后您应该能够删除实体。
答案 1 :(得分:0)
对我来说,答案是将两个实体分成两个不同的表,将公共基表保存在公共字段中,并在每个子表中设置自己有价值的信息。
<joined-subclass name="BaseEntity"
table="t_entity_base"
abstract="true">
<key column="t_uid"></key>
<!-- common mapping -->
<joined-subclass name="EntityA"
table="t_entity_a">
<key column="t_uid"></key>
<!-- specific fields, i.e.: -->
<property name="discriminator" column="discriminator"/>
</joined-subclass>
<joined-subclass name="EntityB"
table="t_entity_b">
<key column="t_uid"></key>
</joined-subclass>
</joined-subclass>
感谢您的帮助。