Your assumption is just not correct: NHibernate couldn't care less if you've
overriden Equals, unless you are using a composite id.
Moreover, even if that were the case, having two objects with the same Id in
the same session will throw.
I could say more but you get the idea.
Diego
On Sat, Dec 25, 2010 at 21:04, cyrus <[email protected]> wrote:
> Hi,
>
> I have the following simple class:
>
> public class Team
> {
> public virtual long id { get; set; }
> public virtual String name { get; set; }
>
>
> public Team() { }
> }
>
> and the following mapping:
>
> <class name="Team" table="Team" >
> <id name="id">
> <generator class="native" />
> </id>
> <property name="name" not-null="true"/>
>
>
> Two Teams with the same name are considered to be the same team.
> Therefore I override the Equals and GetHashCode methods in the
> following manner:
>
>
> public override bool Equals(object other)
> {
> if (other == null) return false;
> Team othert = (Team) other;
> return this.name.Equals(othert.name);
> }
>
> public override int GetHashCode()
> {
> return name.GetHashCode();
> }
>
> Using this code could also cover the case that a transient and a
> persisted object with the same name, are indeed the same object. I
> create two objects:
>
> Team t1 = new Team(); t1.name="aname";
> Team t2 = new Team(); t2.name="aname";
>
> and I save them using the SaveOrUpdate on each object (i.e.
> session.SaveOrUpdate(t1);sessionSaveOrUpdate(t2))
>
> This code results in two entries in the DB with the same name. I would
> expect that NHibernate finds that the two objects are equal (using the
> overriden Equal and GetHashCode methods) and the SaveOrUpdate on t2
> would either update the already inserted object or fail?
>
> --
> 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.