Hi there!
First of all: The title of this posting somehow does not fit the problem...but it describes it quite good (or better as any title I came up with yet), sorry for that.
I am trying to keep my question as easy as possible, though I guess it will be quite hard for me to explain my problem as English is not my native language...
Well, let's try:
I am using NHibernate (Runtime Version is v2.0.50727 according to Visual Studio 2008) in collaboration with Spring.net to access a Microsoft SQL Server holding the data.
Everything is fine with that, I created all the mappings and the mapped classes and - needless to say - the database itself and was able to do all the cool stuff NHibernate and Spring.net are able to do when in collaboration.
The problem is that I am currently trying to implement some business objects (in this current case Addresses, which merges Countries, States, Districts and stuff). The business object IS working, but when I databound it to a datagridview and edited a single row (see example below), all rows with the same value in the same column got an update.
Example
Before
Code:
Country State Something else
Germany Berlin Information...
Italy Italian state Some more info...
Germany Bavaria Even more info
This is, where I edit the first row (with Country == Germany && State == Berlin) and set it's Country-Property to "Ynamreg" (which is Germany backwards...you won't believe how many people do not recognize that *g*).
AfterCode:
Country State Something else
Ynamreg Berlin Information...
Italy Italian state Some more info...
Ynamreg Bavaria Even more info
I will have to mention how the business object works, I think, forgot that in the original posting:
The table "tbl_addresses" holds a foreign key pointing to another table (zipcodes2districts) which itself has a foreign key pointing to a table storing zipcodes which is pointing to the countries-table by a foreign key by itself .
Here's the mapping information for the Address objects (not the business object, which does not have any). Please notice that names are changed and obfuscated here (as I have to).
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="MyAddressClass, MyAssembly" table="tbl_addresses" lazy="false">
<id name="AddressId" type="Guid" unsaved-value="null">
<column name="addresses_id" sql-type="uniqueidentifier" not-null="false" unique="true"/>
<generator class="assigned"/>
</id>
<property name="ParentAddress" type="Guid">
<column name="parent_addresses_id" sql-type="uniqueidentifier" />
</property>
<many-to-one name="Zipcode2District" column="zipcodes2districts_id" class="MyZipcode2DistrictClass, MyAssembly" lazy="false"/>
<property name="AddressName" type="String">
<column name="addresses_name" length="250" sql-type="nvarchar" not-null="true" />
</property>
<property name="AreaName" type="String">
<column name="area_name" length="250" sql-type="nvarchar" not-null="true" />
</property>
</class>
</hibernate-mapping>
I do not blame NHibernate in the first place, but my guess is that NHibernate by default does something like the following:
If any table data is read and like for example the Country-Name-Column is the same like five times (and the business object stores a country-object, not only the name!!) then NHibernate does not create five countries with the same name and identifier (I am using Guids if that is important) but different references in memory, but assigns the same country-object with the same reference to five different business-objects.
Am I right with that? Or is it my fault?
If so - any suggestions of handling that problem? Any workarounds?
Or am I completely wrong and made myself a fool here?
Thanks in advance for ANY help!
Greets - Steps