0

我有两个从同一个表中读取的连接子类,其中一个可空字段作为鉴别器。我已经能够使用这样的子选择读取两个实体:

<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)
4

2 回答 2

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>

然后,您应该能够删除实体。

于 2013-03-16T17:25:30.913 回答
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>

谢谢你的帮助。

于 2013-03-17T23:39:43.427 回答