This is the code I have in my automated persistance test. Part of it was
taken from unhaddins, part from the nh wiki, and I added my own changes. It
tests exactly for that.
In my domain at least, the usual reason for properties that are dirty on
load is having null in the DB and a non-nullable property in .Net.
void TestLoad(string className, ISessionFactory sessionFactory)
{
using (var session = sessionFactory.OpenSession())
{
var entity =
session.CreateCriteria(className).SetMaxResults(1).UniqueResult();
if (entity != null)
{
var sessionImpl = session.GetSessionImplementation();
var persister = sessionImpl.Factory.GetEntityPersister(className);
var oldEntry = sessionImpl.PersistenceContext.GetEntry(
sessionImpl.PersistenceContext.Unproxy(entity));
if (oldEntry.Status != Status.ReadOnly)
{
var oldState = oldEntry.LoadedState;
var currentState = persister.GetPropertyValues(entity,
sessionImpl.EntityMode);
var dirtyProps = persister.FindDirty(currentState, oldState, entity,
sessionImpl);
if (dirtyProps != null)
{
Assert.Fail(
string.Format(
"\n{0}#{1} instance is dirty on load\n{2}",
className,
session.GetIdentifier(entity),
string.Join(
"\n",
dirtyProps.Select(
i => string.Format(
"{0}\n was: {1}\n is: {2}",
persister.PropertyNames[i],
oldState[i] ?? "(null)",
currentState[i] ?? "(null)"))
.ToArray())));
}
}
}
}
}
Diego
On Tue, Dec 15, 2009 at 04:29, John J <[email protected]> wrote:
> I have a situation where of the entities (Patient, in my case) in my
> model seems to be updated every time one of the entities is selected.
> If I issue a query that returns, say, 20 patients, then I will see 20
> update statements in NHProf after the query. (BTW, thanks for a great
> tool Ayende!)
>
> I have had this problem before when I had not mapped enumerated types
> correctly, but it does not seem to be the case here.
>
> My question is, is there a way for me to work out which property is
> causing an object to become dirty? I think the answer might involve
> the FindDirty method in IInterceptor, but I have not been able to
> figure it out...
>
> I'd be grateful for any helpful hints.
>
> --John
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.