Ok, I kind of understood.
I followed this
post<http://ayende.com/blog/3960/nhibernate-mapping-one-to-one>to map correctly
the one-to-one relationship. The child table has a
many-to-one relationship with unique="true", that means only one category
can be assigned to one user. So I suppose the mapping should be correct,
even though I'm not an expert of mapping.
I added a property in the User class like this:
public class User
{
// [...]
private Category _AssignedCategory;
public virtual Category AssignedCategory {
get { return _AssignedCategory; }
set
{
value.AssignedTo = this;
_AssignedCategory = value;
}
}
Now everithing works perfectly.
Thank you.
Bye,
Gianni
Il giorno giovedì 5 luglio 2012 12:37:08 UTC+2, Aleksej Milorava ha scritto:
>
> Hi Gianni,
>
> the first problem that I though of is difference in hbm mapping and C#
> code you defined.
> In the mapping the relation between Category and User is many to one (so
> one category can belong to many users). But in your C# it is one to one, so
> that "u.AssignedCategory = c;" only one category can be assigned to one
> user.
>
>
>
> On Wednesday, July 4, 2012 5:17:00 PM UTC+1, Gianni wrote:
>>
>> Hello,
>>
>> I'm getting stuck while trying to solve a little problem with nhibernate
>> 3.3.1GA and mapping and I hope someone can help me.
>>
>> I have two MySQL tables: User and Category. Each Category *can*reference a
>> User Id in the "AssignedTo" column (i.e. AssignedTo can be
>> NULL):
>>
>> CREATE TABLE User
>> (
>> Id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
>> Name VARCHAR(128) NOT NULL DEFAULT 'Prova' ,
>> LastModified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
>> PRIMARY KEY (Id) ,
>> UNIQUE INDEX Name_UNIQUE (Name ASC)
>> )
>>
>> CREATE TABLE Category
>> (
>> Id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
>> Name VARCHAR(256) NULL ,
>> AssignedTo INT UNSIGNED NULL ,
>> PRIMARY KEY (Id) ,
>> UNIQUE INDEX AssignedTo_UNIQUE (AssignedTo ASC) ,
>> CONSTRAINT fk_Category_User
>> FOREIGN KEY (AssignedTo)
>> REFERENCES User (Id)
>> ON DELETE CASCADE
>> ON UPDATE NO ACTION
>> )
>>
>> And here is the mapping (sorry for the verbosity):
>>
>> <class name="User">
>> <id name="Id" type="System.UInt32" generator="identity">
>> <column name="Id" not-null="true" sql-type="int(10) unsigned" />
>> </id>
>> <property name="Name" not-null="true" unique="true"
>> type="System.String">
>> <column name="Name" length="128" not-null="true" unique="true"
>> sql-type="varchar(128)" />
>> </property>
>> <property name="LastModified" not-null="true" type="System.DateTime">
>> <column name="LastModified" not-null="true" sql-type="timestamp"
>> />
>> </property>
>> <one-to-one name="AssignedCategory" class="Category" cascade="all"
>> foreign-key="fk_Category_User" property-ref="AssignedTo" />
>> </class>
>>
>> <class name="Category">
>> <id name="Id" type="System.UInt32" generator="identity">
>> <column name="Id" not-null="true" sql-type="int(10) unsigned" />
>> </id>
>> <property name="Name" type="System.String">
>> <column name="Name" length="256" sql-type="varchar(256)" />
>> </property>
>> <many-to-one name="AssignedTo" unique="true" class="User"
>> foreign-key="fk_Category_User">
>> <column name="AssignedTo" unique="true" sql-type="int(10)
>> unsigned" />
>> </many-to-one>
>> </class>
>>
>> The problem is that if I assign a User to Category.AssignedTo and then
>> save the Category, the field AssignedTo is filled with the correct User.Id.
>> However, if I assign a Category to a User.AssignedCategory and then save
>> the User, the Category.AssignedTo field is not updated.
>>
>> Here is the c# code and the generated queries:
>>
>> Category c = new Category();
>> c.Name = "General";
>> s.Save(c);
>>
>> User u = new User();
>> u.Name = "Gianni";
>> u.AssignedCategory = c;
>> s.Save(u);
>>
>> s.Flush();
>>
>> Sql:
>>
>> INSERT INTO Category (Name, AssignedTo) VALUES ('General', NULL)
>> SELECT LAST_INSERT_ID()
>> INSERT INTO User (Name, LastModified) VALUES ('Gianni', NULL)
>> SELECT LAST_INSERT_ID()
>>
>> I would have also expected another query updating the foreign key:
>>
>> UPDATE Category SET AssignedTo = 1
>>
>> Everithing else is working fine: every query result is correct and if I do
>>
>> User u = new User();
>> u.Name = "Gianni";
>> s.Save(u);
>>
>> Category c = new Category();
>> c.Name = "General";
>> c.AssignedTo = u;
>> s.Save(c);
>>
>> s.Flush();
>>
>> Then everithing works fine.
>>
>> Can you suggest a solution?
>>
>> Do you think this is a bug or am I doing something wrong?
>>
>> Thank you in advance for any help.
>>
>> Bye,
>> Gianni
>>
>>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/nhusers/-/aSUuwOo-mfkJ.
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.