-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Parent Child problem, non null contraint violation
PostPosted: Fri Jun 10, 2005 3:16 pm 
I've a problem with NHib, I've two objects, which are connected in parent child association, I create the parent and save it to the database, then I add the child, flush everything, and tries to delete the parent.
NHibernate tries to issue an update of the child to set it's parent column to null (which cause constraint violation).
I checked the refenrence, and as far as I can say, I'm doing everything as I should.
As far as I can tell, it should issue a delete statement for the child and the delete the parent.
Here is the code:

Code:
[Test]
public void AddQueryAndThenDelete()
{
   ISession s = repository.Session;
   Project project= new Project(projectName);
   s.Save(project,2);
   Query q = new Query("first query","first query text");
   project.Queries.Add(q);
   q.OwnerProject = project;
   s.Flush();
   s.Delete(project);
   s.Flush();
}



Here are my mapping files:


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Ayende.NHibernateQueryAnalyzer.Model.Project, Ayende.NHibernateQueryAnalyzer.Core" table="projects">
      <id name="Id" column="id" type="Int32" unsaved-value="0" access="nosetter.camelcase">
         <generator class="identity" />
      </id>
      <property name="Name" column="name" type="String" access="nosetter.camelcase" length="255" />
      <set name="Queries" cascade="all-delete-orphan"  inverse="true">
         <key column="project_id"/>
         <one-to-many class="Ayende.NHibernateQueryAnalyzer.Model.Query, Ayende.NHibernateQueryAnalyzer.Core"/>
      </set>
      <bag table="files" name="Files" access="nosetter.camelcase" lazy="true">
         <key column="project_id"/>
         <element type="String" column="filename" />
      </bag>
   </class>
</hibernate-mapping>



And the second:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Ayende.NHibernateQueryAnalyzer.Model.Query, Ayende.NHibernateQueryAnalyzer.Core"
      table="Queries">
      <id name="Id" column="id" type="Int32" unsaved-value="0" access="nosetter.camelcase">
         <generator class="identity" />
      </id>
      <property name="Name" column="name" type="String" length="255" />
      <property name="Text" column="text" type="String" />
      <many-to-one
         not-null="true"
         name="OwnerProject"
         column="project_id"
         class="Ayende.NHibernateQueryAnalyzer.Model.Project, Ayende.NHibernateQueryAnalyzer.Core" />
   </class>
</hibernate-mapping>


Top
  
 
 Post subject: Minor correction
PostPosted: Fri Jun 10, 2005 3:20 pm 
It's not the deletion that fails, it's the insert, NHib tries to do this:
Quote:
2005-06-10 22:10:18,218 [4748] DEBUG NHibernate.Persister.EntityPersister [] <> - Dehydrating entity: Ayende.NHibernateQueryAnalyzer.Model.Query#
2005-06-10 22:10:18,218 [4748] DEBUG NHibernate.Type.NullableType [] <> - binding 'first query' to parameter: 0
2005-06-10 22:10:18,218 [4748] DEBUG NHibernate.Type.NullableType [] <> - binding 'first query text' to parameter: 1
2005-06-10 22:10:18,218 [4748] DEBUG NHibernate.Type.NullableType [] <> - binding null to parameter: 2
2005-06-10 22:10:18,218 [4748] INFO NHibernate.Impl.BatcherImpl [] <> - Preparing INSERT INTO Queries (name, text, project_id) VALUES (@p0, @p1, @p2)


I have no idea why it choose to insert with null. It seems to be ignoring the not-null on Query & the inverse on Project.


Top
  
 
 Post subject: Fixed that..
PostPosted: Fri Jun 10, 2005 3:30 pm 
I should've added a Flush after saving the project.

The real reason in my code was different, but the moment I got to thinking about Flushing, I found it.

He he, I fight with this for over an hour, post, and then find the answer in five minutes :-)
GREAT Forum!


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.