Hi,
I posted a question related to this a few days back but still cannot figure this one out. I have a work around but I would rather it works like it should.
I am getting an error when trying to save an object which has a 1:1 relationship. The object I am trying to save is User which has a property called SecretAnswer.
My C# code is as follows...
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.ActivationCode = RandomPassword.Generate(10);
SecretAnswer sa = new SecretAnswer();
sa.QuestionId = secretQuestionId;
sa.Answer = secretAnswer;
user.SecretAnswer = sa;
userDAO.CreateNewUser(user);
Here is my user mapping file...
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" unsaved-value="{00000000-0000-0000-0000-000000000000}">
<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"/>
<property name="ActivationCode" column="ActivationCode" type="String"/>
<one-to-one
name="SecretAnswer"
class="MLT.Domain.SecretAnswer, MLT.Domain"
constrained="false"
cascade="save-update"
property-ref="UserId" />
</class>
</hibernate-mapping>
Note that I have set constrained to false. When I set it to true nhibernate attempts to execute the SQL inserts in the wrong order. I seem to get the same error no matter which order the inserts are executed though.
I am just doing a session.SaveOrUpdate() on the user object.
It seems to actually work ok for updates, it's just when I first want to add a user for the first time I get this error.
Here is my log...
Code:
2005-06-21 20:43:46,363 [1228] INFO NHibernate.Cfg.Configuration [] <> - searching for mapped documents in assembly: MLT.Domain
2005-06-21 20:43:46,363 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.Contact.hbm.xml
2005-06-21 20:43:46,393 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,403 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.Contact -> ContactList
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.SecretQuestion.hbm.xml
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.SecretQuestion -> SecurityQuestions
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.Journal.hbm.xml
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.Journal -> Journals
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.JournalEntry.hbm.xml
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.JournalEntry -> Entries
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.Group.hbm.xml
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.Group -> Groups
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.User.hbm.xml
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,423 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.User -> Users
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping collection: MLT.Domain.User.FavouriteJournals -> FavouriteJournals
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.SecretAnswer.hbm.xml
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.SecretAnswer -> SecurityAnswer
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Configuration [] <> - searching for mapped documents in assembly: MLT.Domain
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.Contact.hbm.xml
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.Contact -> ContactList
2005-06-21 20:43:46,433 [1228] WARN NHibernate.Cfg.Mappings [] <> - duplicate class mapping: Contact
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.SecretQuestion.hbm.xml
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.SecretQuestion -> SecurityQuestions
2005-06-21 20:43:46,433 [1228] WARN NHibernate.Cfg.Mappings [] <> - duplicate class mapping: SecretQuestion
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.Journal.hbm.xml
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.Journal -> Journals
2005-06-21 20:43:46,433 [1228] WARN NHibernate.Cfg.Mappings [] <> - duplicate class mapping: Journal
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.JournalEntry.hbm.xml
2005-06-21 20:43:46,433 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.JournalEntry -> Entries
2005-06-21 20:43:46,443 [1228] WARN NHibernate.Cfg.Mappings [] <> - duplicate class mapping: JournalEntry
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.Group.hbm.xml
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.Group -> Groups
2005-06-21 20:43:46,443 [1228] WARN NHibernate.Cfg.Mappings [] <> - duplicate class mapping: Group
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.User.hbm.xml
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.User -> Users
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping collection: MLT.Domain.User.FavouriteJournals -> FavouriteJournals
2005-06-21 20:43:46,443 [1228] WARN NHibernate.Cfg.Mappings [] <> - duplicate collection role: MLT.Domain.User.FavouriteJournals
2005-06-21 20:43:46,443 [1228] WARN NHibernate.Cfg.Mappings [] <> - duplicate class mapping: User
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Configuration [] <> - Found mapping documents in assembly: MLT.Domain.SecretAnswer.hbm.xml
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Binder [] <> - Mapping class: MLT.Domain.SecretAnswer -> SecurityAnswer
2005-06-21 20:43:46,443 [1228] WARN NHibernate.Cfg.Mappings [] <> - duplicate class mapping: SecretAnswer
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Configuration [] <> - Configured SessionFactory: NHibernate.Test
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Configuration [] <> - processing one-to-many association mappings
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Configuration [] <> - processing one-to-one association property references
2005-06-21 20:43:46,443 [1228] INFO NHibernate.Cfg.Configuration [] <> - processing foreign key constraints
2005-06-21 20:43:46,453 [1228] INFO NHibernate.Dialect.Dialect [] <> - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2005-06-21 20:43:46,453 [1228] INFO NHibernate.Cfg.SettingsFactory [] <> - use outer join fetching: True
2005-06-21 20:43:46,453 [1228] INFO NHibernate.Connection.ConnectionProviderFactory [] <> - Intitializing connection provider: NHibernate.Connection.DriverConnectionProvider
2005-06-21 20:43:46,453 [1228] INFO NHibernate.Connection.ConnectionProvider [] <> - Configuring ConnectionProvider
2005-06-21 20:43:46,453 [1228] INFO NHibernate.Cfg.SettingsFactory [] <> - Query language substitutions: yes='Y';true=1;no='N';false=0;
2005-06-21 20:43:46,453 [1228] INFO NHibernate.Cfg.SettingsFactory [] <> - cache provider: NHibernate.Cache.HashtableCacheProvider
2005-06-21 20:43:46,453 [1228] INFO NHibernate.Cfg.Configuration [] <> - instantiating and configuring caches
2005-06-21 20:43:46,463 [1228] INFO NHibernate.Impl.SessionFactoryImpl [] <> - building session factory
2005-06-21 20:43:46,524 [1228] INFO NHibernate.Impl.SessionFactoryObjectFactory [] <> - Factory name:NHibernate.Test
2005-06-21 20:43:46,634 [1228] INFO NHibernate.Loader.Loader [] <> - SELECT this.UID as UID1_, this.lastName as lastName1_, this.firstName as firstName1_, this.password as password1_, this.ActivationCode as Activat11_1_, this.creationDate as creation7_1_, this.ReceiveComments as Receive10_1_, this.email as email1_, this.Username as Username1_, this.PrivateStatus as PrivateS8_1_, this.Active as Active1_, secretan1_.SecurityAnswerID as Security1_0_, secretan1_.UID as UID0_, secretan1_.QuestionID as QuestionID0_, secretan1_.Answer as Answer0_ FROM Users this left outer join SecurityAnswer secretan1_ on this.UID=secretan1_.UID WHERE (this.email = @p0)
2005-06-21 20:43:46,634 [1228] INFO NHibernate.Impl.BatcherImpl [] <> - Preparing SELECT this.UID as UID1_, this.lastName as lastName1_, this.firstName as firstName1_, this.password as password1_, this.ActivationCode as Activat11_1_, this.creationDate as creation7_1_, this.ReceiveComments as Receive10_1_, this.email as email1_, this.Username as Username1_, this.PrivateStatus as PrivateS8_1_, this.Active as Active1_, secretan1_.SecurityAnswerID as Security1_0_, secretan1_.UID as UID0_, secretan1_.QuestionID as QuestionID0_, secretan1_.Answer as Answer0_ FROM Users this left outer join SecurityAnswer secretan1_ on this.UID=secretan1_.UID WHERE (this.email = @p0)
2005-06-21 20:43:46,644 [1228] INFO NHibernate.Loader.Loader [] <> - SELECT this.UID as UID1_, this.lastName as lastName1_, this.firstName as firstName1_, this.password as password1_, this.ActivationCode as Activat11_1_, this.creationDate as creation7_1_, this.ReceiveComments as Receive10_1_, this.email as email1_, this.Username as Username1_, this.PrivateStatus as PrivateS8_1_, this.Active as Active1_, secretan1_.SecurityAnswerID as Security1_0_, secretan1_.UID as UID0_, secretan1_.QuestionID as QuestionID0_, secretan1_.Answer as Answer0_ FROM Users this left outer join SecurityAnswer secretan1_ on this.UID=secretan1_.UID WHERE (this.Username = @p0)
2005-06-21 20:43:46,644 [1228] INFO NHibernate.Impl.BatcherImpl [] <> - Preparing SELECT this.UID as UID1_, this.lastName as lastName1_, this.firstName as firstName1_, this.password as password1_, this.ActivationCode as Activat11_1_, this.creationDate as creation7_1_, this.ReceiveComments as Receive10_1_, this.email as email1_, this.Username as Username1_, this.PrivateStatus as PrivateS8_1_, this.Active as Active1_, secretan1_.SecurityAnswerID as Security1_0_, secretan1_.UID as UID0_, secretan1_.QuestionID as QuestionID0_, secretan1_.Answer as Answer0_ FROM Users this left outer join SecurityAnswer secretan1_ on this.UID=secretan1_.UID WHERE (this.Username = @p0)
2005-06-21 20:43:46,674 [1228] INFO NHibernate.Impl.BatcherImpl [] <> - Preparing INSERT INTO Users (lastName, firstName, password, ActivationCode, creationDate, ReceiveComments, email, Username, PrivateStatus, Active, UID) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)
2005-06-21 20:43:46,684 [1228] INFO NHibernate.Impl.BatcherImpl [] <> - Preparing INSERT INTO SecurityAnswer (UID, QuestionID, Answer, SecurityAnswerID) VALUES (@p0, @p1, @p2, @p3)
2005-06-21 20:43:46,684 [1228] ERROR NHibernate.ADOException [] <> - could not synchronize database state with session
Exception: System.Data.SqlClient.SqlException
Message: INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_SecurityAnswer_Users'. The conflict occurred in database 'MLOT', table 'Users', column 'UID'.
The statement has been terminated.
Source: .Net SqlClient Data Provider
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at NHibernate.Impl.BatcherImpl.ExecuteNonQuery(IDbCommand cmd)
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(Int32 expectedRowCount)
at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledInsertion.Execute()
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()
Any help would be appreciated.