I have a very simple table in an SQL server that is mapped in a C# POCO object, with the following code:
public class DataTable : Entity<Guid>
{
public virtual int itemId { get; set; }
public virtual string data { get; set; }
public virtual DateTime lastDate { get; set; }
public virtual string lastUser { get; set; }
}
public class DataTableMap : ClassMap<DataTable>
{
public DataTableMap()
{
Map(x => x.itemId);
Map(x => x.data);
Map(x => x.lastDate);
Map(x => x.lastUser);
}
}
I would like to track changes, using an Hibernate interceptor, so that I can add some info in another SQL server table is some of the value occurs (E.g. data is empty or lastUser is Admin).
I have implemented a simple interceptor:
public class TrackingEntityInterceptor : EmptyInterceptor
{
public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
var persistentObject = entity as IPersistentObject;
if (persistentObject != null)
{
persistentObject.OnSave();
}
return false;
}
public override SqlString OnPrepareStatement(SqlString sql)
{
ApplicationBlocks.Logging.Log.Debug($"Executing sql: {sql.ToString()}");
return sql;
}
}
But I am not able to access the values of the Entity that is saved, nor I am able to active the interceptor only when that specific Entity is saved.
Is there a way to have a function called when that Entity is being saved, that allows me to inspect the values that are about to be saved and eventually apply some other changes to the DB ?