.netxmlnhibernatenhibernate-mapping

Is there a way to put conditions on listing via mapping on nhibernate 2.2?


I'm working on a legacy system for which the code has no documentation and follows no patterns.

Some parts of the system do queries like session.Query<Entity>();, other parts do Repository.GetAll();.

The code that uses Repository is organized and I don't have any problem there, but the part that uses session is a mess, there is a lot of logic and queries in the controllers for almost every entity.

Recently we needed to make a change in one entity and add an column called active.

MyClass.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<class name="my_class" table="my_class" lazy="true">
    <id name="Id" column="id" type="Int32" unsaved-value="0">
    <generator class="native">
        <param name="sequence">sq_my_class</param>
    </generator>
    </id>
    
    <property column="name" type="String" name="Name" not-null="true" length="200" />
    <property column="area" type="Double" name="Area" not-null="true" />
    <property column="type" type="Int32" name="Type" not-null="true" />
    <property column="fiscalization" type="Boolean" name="Fiscalization" not-null="true" />         
    <property column="active" type="Boolean" name="Active" not-null="true" />
</class>

I want to prevent listing any object that is not activated, but I don't want to search the entire code for queries on this entity because I'm afraid of leaving something behind, instead, I wanted to know if I can do this via mapping.


Solution

  • Try setting where="active = 1" on class element:

    <class name="my_class" table="my_class" lazy="true" where="active = 1" >