NHibernate <Join/> with filters
I was hoping there would be a simple answer to this one....
I have a global filter designed to avoid soft deletes in the database, this works fine in the rest of my system. However we are using a legacy database with an existing "UserBase" table, any new properties we required we added to a "User" table and mapped our User class to retrieve data from 2 tables using the "join" mapping - this way we are not altering the existing "UserBase" table.
Again this works fine, until we try to apply the soft delete filter to the class - as the generated SQL applies the filter to the "BaseUser" table where there is no matching column.
<class name="User" table="UserBase">
<id name="Id" column="userid" type="Int32" unsaved-value="-1">
<generator class="native">
<param name="sequence"></param>
</generator>
</id>
<property name="Email" column="UserEmail" type="String" length="100" />
.........
.........
<join table="User" optional="false">
<key column="UserID" />
<property name="TimeZone" column="timezone" type="Int32" />
.........
.........
<property name="IsDeleted" column="IsDeleted" type="Boolean" />
</join>
<filter name="AvoidLogicalDeleted" condition="IsDeleted = 0" />
</class>
Is there any way that I can get the filter to apply to the "joined" table, rather than the class table?
I have also tried to specify a "subselect" in the "join" mapping with a where clause ignoring soft deletes, but it appears to be ignored!?
A point in the right direction 开发者_如何学编程would be greatly appreciated....
I'm not sure I have an answer so much as a work around. Instead of mapping the User class to the UserBase table and then joined to the User table, what about using a view? Perhaps you can create a view in the database called UserView which has the UserBase and User tables joined and then map the User class to that view. NHibernate should then treat the IsDeleted column like any other column and the filter should work.
Another option that occurred to me is to swap the mapping of the User class from UserBase to User. The IsDeleted column will then no longer be in the joined table but in the main table connected to User. Then, the fact that the filter isn't working on a joined table won't matter.
精彩评论