Hello,
I've got a problem with SessionImpl not evicting newly created objects... but first thing first...
1. My objects have composite Ids
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="hibernate.UserWord, hibernate" table="user_word">
<composite-id>
<key-property name="UserId" column="user_id" />
<key-property name="Word" column="word" />
</composite-id>
<property name="Score" column="score"></property>
</class>
</hibernate-mapping>
2. In a loop I create new object and do
try {
session.Save(uw); session.Flush();
}
catch (Exception e)
{
session.Evict(uw);
}
I do end up with duplicate objects and get the exception, so session.Evict() is called and I see object removed, but session.insertions still holds something about that duplicate object I am evicting. So next time I call session.Flush() I get the same exception again and again....
Work around is to recreate the session instead of evicting bad object, but this breaks transactions that I want (I did started with transactions and the example above is simplified version so the problem would be obvious)
Just to be sure I implemented
public override bool Equals(Object obj)
{
if (obj == null || GetType() != obj.GetType()) return false;
UserWord uw = (UserWord)obj;
return Word.Equals(uw.Word) && UserId.Equals(uw.UserId);
}
and
public override int GetHashCode ()
{
return Word.GetHashCode() ^ UserId.GetHashCode();
}
but it didn't change anything.
Since I am a novice with NHibernate I am not sure if it is a bug or I am abusing NHibernate is some way, but I don't see what I am doing wrong.
Any ideas?
Thanks
|