-->
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: Can not delete data which has one-to-many relationship
PostPosted: Mon Jan 09, 2006 5:13 am 
Newbie

Joined: Mon Jan 09, 2006 4:57 am
Posts: 18
Hibernate version: 1.0.1.0

Mapping documents:

Model.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="UIProgram.ModelClasses.Model, UIProgram" table="T_Model_Master">
    <id name="Id" column="Model_ID" type="Int32" unsaved-value="null">
      <generator class="identity" />
    </id>

    <property name="Name" column="Model_Name" type="String" length="50" />
    <property name="Info" column="Model_Info" type="String" length="255" />
    <property name="Type" column="Model_Type" type="Int32" />

    <set name="Shelves" cascade="all" lazy="false" inverse="true">
      <key column="Model_Id" />
      <one-to-many class="UIProgram.ModelClasses.Shelf, UIProgram" />
    </set>
  </class>
</hibernate-mapping>


Shelf.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="UIProgram.ModelClasses.Shelf, UIProgram" table="T_Shelf_Info">
    <id name="Id" column="Shelf_ID" type="Int32">
      <generator class="identity" />
    </id>

    <property name="Name" column="Shelf_Name" type="String" length="50" />
   
    <component name="Location" class="UIProgram.ModelClasses.Point, UIProgram">
      <property name="X" column="X_Coord" type="Int32" />
      <property name="Y" column="Y_Coord" type="Int32" />
    </component>
   
    <property name="Width" column="Width" type="Int32" />
    <property name="Height" column="Height" type="Int32" />

    <many-to-one name="Model" column="Model_Id" not-null="true" />
  </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
ICriteria criteria = session.CreateCriteria(typeof(Model));
criteria.Add(new EqExpression("Id", 44));

IList models = criteria.List();

Model model = (Model)models[0];

ITransaction tx = session.BeginTransaction();

session.Delete(model);
tx.Commit();


Full stack trace of any exception that occurs:

Code:
NHibernate.ADOException was unhandled
  Message="could not delete: [UIProgram.ModelClasses.Shelf#84]"
  Source="NHibernate"
  StackTrace:
       at NHibernate.Persister.EntityPersister.Delete(Object id, Object version, Object obj, ISessionImplementor session)
       at NHibernate.Impl.ScheduledDeletion.Execute()
       at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
       at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
       at NHibernate.Impl.SessionImpl.Execute()
       at NHibernate.Impl.SessionImpl.Flush()
       at NHibernate.Transaction.AdoTransaction.Commit()
       at UIProgram.Test.DeleteModel() in D:\_Works\_NET\CSharp\UIProgram\Test.cs:line 55
       at UIProgram.Test.Test_Load(Object sender, EventArgs e) in D:\_Works\_NET\CSharp\UIProgram\Test.cs:line 33
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
       at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Control.set_Visible(Boolean value)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at UIProgram.Program.Main() in D:\_Works\_NET\CSharp\UIProgram\Program.cs:line 19
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()


Name and version of the database you are using: MS SQL Server 2000 with SP4

If I create a new model without shelves, I can delete it. But if I create a model with shelves, I can not delete the model.

It raised the exception when I try to commit transaction.

Help me! Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 5:47 am 
Newbie

Joined: Mon Jan 09, 2006 4:57 am
Posts: 18
I found that I can delete all shelves except the last one.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 6:55 am 
Newbie

Joined: Mon Jan 09, 2006 4:57 am
Posts: 18
My problem is database designing, not programming. I use non-identifying relationship so I cannot delete the last row in "many" table.


Top
 Profile  
 
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.