Hi,
I am new to nhibernate and I have been trying to solve this for hours with no luck so I am hoping someone here can help me with this.
I have a simple one-to-one mapping from a User class to a SecretAnswer class. When I save the User object I expect a new User to be inserted into the Users table and then a new SecretAnswer to be inserted into another table.
The error I get is "SQL update or deletion failed (row not found)". When I look in the log I can see that nhiberante has attempted to execute an insert for the User object and then an update for the SecurityAnswer object. Obviously the update does not work because the row doesn't exist yet!
I expect 2 insert statements to be executed and I cannot work out why this is not happening.
Here are my mappings...
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="MLT.Domain.User, MLT.Domain" table="Users">
<id name="Id" column="UID" type="Guid">
<generator class="guid" />
</id>
<property name="FirstName" column="firstName" type="String" length="50"/>
<property name="LastName" column="lastName" type="String" length="50"/>
<property name="Password" column="password" type="String" length="50"/>
<property name="Username" column="Username" type="String" length="50"/>
<property name="Email" column="email" type="String" length="255"/>
<property name="CreationDate" column="creationDate" type="DateTime"/>
<property name="AccountPrivate" column="PrivateStatus" type="Boolean"/>
<property name="AccountActive" column="Active" type="Boolean"/>
<property name="ReceiveCommentsEmail" column="ReceiveComments" type="Boolean"/>
<one-to-one
name="SecretAnswer"
class="MLT.Domain.SecretAnswer, MLT.Domain"
cascade="all"
constrained="true"
property-ref="UserId" />
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="MLT.Domain.SecretAnswer, MLT.Domain" table="SecurityAnswer">
<id name="SecretAnswerId" column="SecurityAnswerID" type="Guid">
<generator class="guid" />
</id>
<property name="UserId" column="UID" type="Guid" />
<property name="QuestionId" column="QuestionID" type="Int32" />
<property name="Answer" column="Answer" type="String" />
</class>
</hibernate-mapping>
The code I am using to do the save is below...
Code:
User user = new User();
user.FirstName = firstName;
user.LastName = lastName;
user.Email = email;
user.Password = password;
user.AccountActive = false;
user.AccountPrivate = privateAccount;
user.CreationDate = DateTime.Now;
user.ReceiveCommentsEmail = receiveCommentsEmail;
user.Username = username;
//user.LastLogon = DateTime.Now;
SecretAnswer sa = new SecretAnswer();
sa.QuestionId = secretQuestionId;
sa.Answer = secretAnswer;
sa.UserId = user.Id;
sa.User = user;
user.SecretAnswer = sa;
userDAO.SaveNewUser(user);
Note that the UserDAO just calls session.Save() passing in the user object.
If someone can tell me what I am doing wrong that would be great. At the moment I have a work around which is to save the objects separately which I think is not a good solution.
[/code]