I've got a class with a byte[] array property that maps to a SQL Server table with an image column. When I save a new instance of this class it results in both an insert and update being sent to the database. If I read this record out and immediately save it without modifying anything, an update is sent to the database. If you look at the output below, you'll see that saving Cat only results in a single insert, while Dog (with the byte[] array) results in an insert and 2 updates. I believe that the problem can be traced to the EqualsHelper.cs class. Comparing the currentstate and previousstate byte arrays results in comparing the variable reference rather than the values. Is there something that I'm doing wrong here?
This is using NHibernate 2.0 Alpha 1.
Here is my mapping file.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="ClassLibrary1" assembly="ClassLibrary1">
<class name="Dog" table="Dog">
<id name="Id">
<column name="Id" sql-type="uniqueidentifier" not-null="true"/>
<generator class="guid.comb" />
</id>
<property name="Data" column="Data" type="BinaryBlob" />
</class>
<class name="Cat" table="Cat">
<id name="Id">
<column name="Id" sql-type="uniqueidentifier" not-null="true"/>
<generator class="guid.comb" />
</id>
</class>
</hibernate-mapping>
Here are my mapped classesCode:
namespace ClassLibrary1
{
public class Dog
{
private Guid id;
private byte[] data;
public virtual Guid Id
{
get{ return id; }
set{ id = value; }
}
public virtual byte[] Data
{
get { return data; }
set { data = value; }
}
}
public class Cat
{
private Guid id;
public virtual Guid Id
{
get { return id; }
set { id = value; }
}
}
}
Here is the code that I'm executing.Code:
ISessionFactory sf = new Configuration().Configure().BuildSessionFactory();
ISession s = sf.OpenSession();
ITransaction tx = s.BeginTransaction();
Cat cat1 = new Cat();
Console.WriteLine("Should insert Cat");
s.Save(cat1);
tx.Commit();
Cat cat2 = s.Get<Cat>(cat1.Id);
tx = s.BeginTransaction();
Console.WriteLine("Should not update Cat");
s.Save(cat2);
tx.Commit();
Dog dog1 = new Dog();
dog1.Data = new byte[] {1, 2, 3};
tx = s.BeginTransaction();
Console.WriteLine("Should insert Dog");
s.Save(dog1);
tx.Commit();
Dog dog2 = s.Get<Dog>(dog1.Id);
tx = s.BeginTransaction();
Console.WriteLine("Should not update Dog");
s.Save(dog2);
tx.Commit();
s.Close();
Here is my configuration Code:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Server=(local);initial catalog=NHibernateTest;Integrated Security=SSPI</property>
<property name="show_sql">true</property>
<mapping assembly="ClassLibrary1" />
</session-factory>
</hibernate-configuration>
Here is the console output displaying the sql executed.
Should insert Cat
NHibernate: INSERT INTO Cat (Id) VALUES (@p0); @p0 = '3000ffd5-4d07-4397-8d2c-9a78016f0133'
Should not update Cat
Should insert Dog
NHibernate: INSERT INTO Dog (Data, Id) VALUES (@p0, @p1); @p0 = 'System.Byte[]', @p1 = '0044e33c-dd6a-4403-ae38-9a78016f0170'
NHibernate: UPDATE Dog SET Data = @p0 WHERE Id = @p1; @p0 = 'System.Byte[]', @p1 = '0044e33c-dd6a-4403-ae38-9a78016f0170'
Should not update Dog
NHibernate: UPDATE Dog SET Data = @p0 WHERE Id = @p1; @p0 = 'System.Byte[]', @p1 = '0044e33c-dd6a-4403-ae38-9a78016f0170'