Index: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Convert.cs
===================================================================
--- branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Convert.cs	(revision 5955)
+++ branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Convert.cs	(revision 6006)
@@ -21,4 +21,5 @@
 
 using System;
+using System.Collections.Generic;
 using System.Data.Linq;
 using System.Linq;
@@ -37,12 +38,13 @@
         ParentJobId = source.ParentJobId,
         Priority = source.Priority,
-        PluginsNeededIds = source.RequiredPlugins.Select(x => x.PluginId).ToList(),
+        PluginsNeededIds = (source.RequiredPlugins == null ? new List<Guid>() : source.RequiredPlugins.Select(x => x.PluginId).ToList()),
         LastHeartbeat = source.LastHeartbeat,
         State = source.State,
-        StateLog = source.StateLogs.Select(x => Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList(),
+        StateLog = (source.StateLogs == null ? new List<DT.StateLog>() : source.StateLogs.Select(x => Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList()),
         IsParentJob = source.IsParentJob,
         FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished,
         Command = source.Command,
-        LastJobDataUpdate = source.JobData.LastUpdate
+        LastJobDataUpdate = (source.JobData == null ? DateTime.MinValue : source.JobData.LastUpdate),
+        HiveExperimentId = source.HiveExperimentId
       };
     }
@@ -71,4 +73,5 @@
         target.Command = source.Command;
         // RequiredPlugins are added by Dao
+        target.HiveExperimentId = source.HiveExperimentId;
       }
     }
@@ -129,5 +132,5 @@
     public static DT.HiveExperiment ToDto(HiveExperiment source) {
       if (source == null) return null;
-      return new DT.HiveExperiment { Id = source.HiveExperimentId, Description = source.Description, Name = source.Name, RootJobId = source.RootJobId, OwnerUserId = source.OwnerUserId, DateCreated = source.DateCreated, ResourceNames = source.ResourceIds, LastAccessed = source.LastAccessed };
+      return new DT.HiveExperiment { Id = source.HiveExperimentId, Description = source.Description, Name = source.Name, OwnerUserId = source.OwnerUserId, DateCreated = source.DateCreated, ResourceNames = source.ResourceIds, LastAccessed = source.LastAccessed };
     }
     public static HiveExperiment ToEntity(DT.HiveExperiment source) {
@@ -138,5 +141,5 @@
     public static void ToEntity(DT.HiveExperiment source, HiveExperiment target) {
       if ((source != null) && (target != null)) {
-        target.HiveExperimentId = source.Id; target.Description = source.Description; target.Name = source.Name; target.RootJobId = source.RootJobId; target.OwnerUserId = source.OwnerUserId; target.DateCreated = source.DateCreated; target.ResourceIds = source.ResourceNames; target.LastAccessed = source.LastAccessed;
+        target.HiveExperimentId = source.Id; target.Description = source.Description; target.Name = source.Name; target.OwnerUserId = source.OwnerUserId; target.DateCreated = source.DateCreated; target.ResourceIds = source.ResourceNames; target.LastAccessed = source.LastAccessed;
       }
     }
Index: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDao.cs
===================================================================
--- branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDao.cs	(revision 5955)
+++ branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDao.cs	(revision 6006)
@@ -237,7 +237,9 @@
 
     private DT.HiveExperiment AddStatsToExperiment(HiveDataContext db, DT.HiveExperiment exp) {
-      var jobs = new List<Job>();
-      CollectChildJobs(db, exp.RootJobId, jobs);
-      exp.JobCount = jobs.Count;
+      if (exp == null)
+        return null;
+
+      var jobs = db.Jobs.Where(j => j.HiveExperimentId == exp.Id);
+      exp.JobCount = jobs.Count();
       exp.CalculatingCount = jobs.Count(j => j.State == JobState.Calculating);
       exp.FinishedCount = jobs.Count(j => j.State == JobState.Finished);
@@ -575,10 +577,5 @@
     public Guid GetExperimentForJob(Guid jobId) {
       using (var db = CreateContext()) {
-        var job = db.Jobs.SingleOrDefault(j => j.JobId == jobId);
-        if (job.ParentJobId.HasValue) {
-          return GetExperimentForJob(job.ParentJobId.Value);
-        } else {
-          return db.HiveExperiments.SingleOrDefault(he => he.RootJobId == jobId).HiveExperimentId;
-        }
+        return db.Jobs.Single(j => j.JobId == jobId).HiveExperimentId;
       }
     }
Index: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.dbml
===================================================================
--- branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.dbml	(revision 5955)
+++ branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.dbml	(revision 6006)
@@ -70,4 +70,5 @@
       <Column Name="FinishWhenChildJobsFinished" Type="System.Boolean" DbType="Bit" CanBeNull="false" />
       <Column Name="Command" Type="global::HeuristicLab.Services.Hive.Common.DataTransfer.Command?" DbType="VarChar(30)" CanBeNull="true" />
+      <Column Name="HiveExperimentId" Type="System.Guid" DbType="UniqueIdentifier NOT NULL" CanBeNull="false" />
       <Association Name="Job_AssignedResource" Member="AssignedResources" ThisKey="JobId" OtherKey="JobId" Type="AssignedResource" />
       <Association Name="Job_RequiredPlugin" Member="RequiredPlugins" ThisKey="JobId" OtherKey="JobId" Type="RequiredPlugin" />
@@ -76,4 +77,5 @@
       <Association Name="Job_StateLog" Member="StateLogs" ThisKey="JobId" OtherKey="JobId" Type="StateLog" />
       <Association Name="Job_Job" Member="ParentJob" Storage="_Job1" ThisKey="ParentJobId" OtherKey="JobId" Type="Job" IsForeignKey="true" />
+      <Association Name="HiveExperiment_Job" Member="HiveExperiment" ThisKey="HiveExperimentId" OtherKey="HiveExperimentId" Type="HiveExperiment" IsForeignKey="true" />
     </Type>
   </Table>
@@ -97,10 +99,9 @@
       <Column Name="ResourceIds" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
       <Column Name="OwnerUserId" Storage="_UserId" Type="System.Guid" DbType="UniqueIdentifier" CanBeNull="false" />
-      <Column Name="RootJobId" Type="System.Guid" DbType="UniqueIdentifier" CanBeNull="false" />
       <Column Name="DateCreated" Type="System.DateTime" DbType="DateTime" CanBeNull="false" />
       <Column Name="LastAccessed" Type="System.DateTime" DbType="DateTime" CanBeNull="true" />
       <Column Name="IsHiveEngine" Type="System.Boolean" DbType="Bit" CanBeNull="false" />
       <Association Name="HiveExperiment_HiveExperimentPermission" Member="HiveExperimentPermissions" ThisKey="HiveExperimentId" OtherKey="HiveExperimentId" Type="HiveExperimentPermission" />
-      <Association Name="Job_HiveExperiment" Member="RootJob" Storage="_Job" ThisKey="RootJobId" OtherKey="JobId" Type="Job" IsForeignKey="true" DeleteRule="CASCADE" />
+      <Association Name="HiveExperiment_Job" Member="Jobs" ThisKey="HiveExperimentId" OtherKey="HiveExperimentId" Type="Job" />
     </Type>
   </Table>
Index: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.dbml.layout
===================================================================
--- branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.dbml.layout	(revision 5955)
+++ branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.dbml.layout	(revision 6006)
@@ -27,8 +27,8 @@
       </nestedChildShapes>
     </classShape>
-    <classShape Id="695bfc39-59f3-4e60-8644-f847964bf62c" absoluteBounds="6.5, 1, 2, 2.9247054036458331">
+    <classShape Id="695bfc39-59f3-4e60-8644-f847964bf62c" absoluteBounds="6.5, 1, 2, 3.1170068359374996">
       <DataClassMoniker Name="/HiveDataContext/Job" />
       <nestedChildShapes>
-        <elementListCompartment Id="a6a30e11-03d1-4869-82e6-b733f4ef9974" absoluteBounds="6.5150000000000006, 1.46, 1.9700000000000002, 2.364705403645833" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
+        <elementListCompartment Id="a6a30e11-03d1-4869-82e6-b733f4ef9974" absoluteBounds="6.5150000000000006, 1.46, 1.9700000000000002, 2.5570068359375" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
       </nestedChildShapes>
     </classShape>
@@ -39,8 +39,8 @@
       </nestedChildShapes>
     </classShape>
-    <classShape Id="e6f840cc-2968-4be1-b234-eef624ccacbb" absoluteBounds="4.125, 2.625, 2, 2.5401025390624996">
+    <classShape Id="e6f840cc-2968-4be1-b234-eef624ccacbb" absoluteBounds="4.125, 2.625, 2, 2.3478011067708326">
       <DataClassMoniker Name="/HiveDataContext/HiveExperiment" />
       <nestedChildShapes>
-        <elementListCompartment Id="0c65d4e1-256a-4a91-9a57-392f25e4de7f" absoluteBounds="4.1400000000000006, 3.0850000000000009, 1.9700000000000002, 1.9801025390625" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
+        <elementListCompartment Id="0c65d4e1-256a-4a91-9a57-392f25e4de7f" absoluteBounds="4.1400000000000006, 3.0850000000000009, 1.9700000000000002, 1.7878011067708333" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
       </nestedChildShapes>
     </classShape>
@@ -76,5 +76,5 @@
       </nodes>
     </associationConnector>
-    <associationConnector edgePoints="[(7.98437325 : 3.92470540364583); (7.98437325 : 4.59300017447917); (8.875 : 4.59300017447917)]" fixedFrom="Algorithm" fixedTo="Algorithm">
+    <associationConnector edgePoints="[(7.8398428125 : 4.1170068359375); (7.8398428125 : 4.689150890625); (8.875 : 4.689150890625)]" fixedFrom="Algorithm" fixedTo="Algorithm">
       <AssociationMoniker Name="/HiveDataContext/Job/Job_AssignedResource" />
       <nodes>
@@ -83,5 +83,5 @@
       </nodes>
     </associationConnector>
-    <associationConnector edgePoints="[(7.179686625 : 3.92470540364583); (7.179686625 : 5.5)]" fixedFrom="Algorithm" fixedTo="Algorithm">
+    <associationConnector edgePoints="[(7.10742140625 : 4.1170068359375); (7.10742140625 : 5.5)]" fixedFrom="Algorithm" fixedTo="Algorithm">
       <AssociationMoniker Name="/HiveDataContext/Job/Job_RequiredPlugin" />
       <nodes>
@@ -102,11 +102,4 @@
         <classShapeMoniker Id="706a4581-6daf-4e71-ae2a-87d50b27a051" />
         <classShapeMoniker Id="8d5712f7-7a1a-4a89-bd4d-fd60200d3306" />
-      </nodes>
-    </associationConnector>
-    <associationConnector edgePoints="[(6.5 : 3.27485270182292); (6.125 : 3.27485270182292)]" fixedFrom="Algorithm" fixedTo="Algorithm">
-      <AssociationMoniker Name="/HiveDataContext/Job/Job_HiveExperiment" />
-      <nodes>
-        <classShapeMoniker Id="695bfc39-59f3-4e60-8644-f847964bf62c" />
-        <classShapeMoniker Id="e6f840cc-2968-4be1-b234-eef624ccacbb" />
       </nodes>
     </associationConnector>
@@ -177,5 +170,5 @@
       </nodes>
     </associationConnector>
-    <associationConnector edgePoints="[(4.125 : 4.39505126953125); (3.75 : 4.39505126953125)]" fixedFrom="NotFixed" fixedTo="NotFixed">
+    <associationConnector edgePoints="[(4.125 : 4.29890055338542); (3.75 : 4.29890055338542)]" fixedFrom="Algorithm" fixedTo="Algorithm">
       <AssociationMoniker Name="/HiveDataContext/HiveExperiment/HiveExperiment_HiveExperimentPermission" />
       <nodes>
@@ -190,4 +183,11 @@
       </nestedChildShapes>
     </classShape>
+    <associationConnector edgePoints="[(6.125 : 3.37100341796875); (6.5 : 3.37100341796875)]" fixedFrom="Algorithm" fixedTo="Algorithm">
+      <AssociationMoniker Name="/HiveDataContext/HiveExperiment/HiveExperiment_Job" />
+      <nodes>
+        <classShapeMoniker Id="e6f840cc-2968-4be1-b234-eef624ccacbb" />
+        <classShapeMoniker Id="695bfc39-59f3-4e60-8644-f847964bf62c" />
+      </nodes>
+    </associationConnector>
   </nestedChildShapes>
 </ordesignerObjectsDiagram>
Index: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.designer.cs
===================================================================
--- branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.designer.cs	(revision 5955)
+++ branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDataContext.designer.cs	(revision 6006)
@@ -1424,4 +1424,6 @@
 		private global::HeuristicLab.Services.Hive.Common.DataTransfer.Command? _Command;
 		
+		private System.Guid _HiveExperimentId;
+		
 		private EntitySet<AssignedResource> _AssignedResources;
 		
@@ -1435,4 +1437,6 @@
 		
 		private EntityRef<Job> _Job1;
+		
+		private EntityRef<HiveExperiment> _HiveExperiment;
 		
     #region Extensibility Method Definitions
@@ -1462,4 +1466,6 @@
     partial void OnCommandChanging(global::HeuristicLab.Services.Hive.Common.DataTransfer.Command? value);
     partial void OnCommandChanged();
+    partial void OnHiveExperimentIdChanging(System.Guid value);
+    partial void OnHiveExperimentIdChanged();
     #endregion
 		
@@ -1472,4 +1478,5 @@
 			this._StateLogs = new EntitySet<StateLog>(new Action<StateLog>(this.attach_StateLogs), new Action<StateLog>(this.detach_StateLogs));
 			this._Job1 = default(EntityRef<Job>);
+			this._HiveExperiment = default(EntityRef<HiveExperiment>);
 			OnCreated();
 		}
@@ -1699,4 +1706,28 @@
 		}
 		
+		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_HiveExperimentId", DbType="UniqueIdentifier NOT NULL")]
+		public System.Guid HiveExperimentId
+		{
+			get
+			{
+				return this._HiveExperimentId;
+			}
+			set
+			{
+				if ((this._HiveExperimentId != value))
+				{
+					if (this._HiveExperiment.HasLoadedOrAssignedValue)
+					{
+						throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
+					}
+					this.OnHiveExperimentIdChanging(value);
+					this.SendPropertyChanging();
+					this._HiveExperimentId = value;
+					this.SendPropertyChanged("HiveExperimentId");
+					this.OnHiveExperimentIdChanged();
+				}
+			}
+		}
+		
 		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_AssignedResource", Storage="_AssignedResources", ThisKey="JobId", OtherKey="JobId")]
 		public EntitySet<AssignedResource> AssignedResources
@@ -1810,4 +1841,38 @@
 					}
 					this.SendPropertyChanged("ParentJob");
+				}
+			}
+		}
+		
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="HiveExperiment_Job", Storage="_HiveExperiment", ThisKey="HiveExperimentId", OtherKey="HiveExperimentId", IsForeignKey=true)]
+		public HiveExperiment HiveExperiment
+		{
+			get
+			{
+				return this._HiveExperiment.Entity;
+			}
+			set
+			{
+				HiveExperiment previousValue = this._HiveExperiment.Entity;
+				if (((previousValue != value) 
+							|| (this._HiveExperiment.HasLoadedOrAssignedValue == false)))
+				{
+					this.SendPropertyChanging();
+					if ((previousValue != null))
+					{
+						this._HiveExperiment.Entity = null;
+						previousValue.Jobs.Remove(this);
+					}
+					this._HiveExperiment.Entity = value;
+					if ((value != null))
+					{
+						value.Jobs.Add(this);
+						this._HiveExperimentId = value.HiveExperimentId;
+					}
+					else
+					{
+						this._HiveExperimentId = default(System.Guid);
+					}
+					this.SendPropertyChanged("HiveExperiment");
 				}
 			}
@@ -2146,6 +2211,4 @@
 		private System.Guid _UserId;
 		
-		private System.Guid _RootJobId;
-		
 		private System.DateTime _DateCreated;
 		
@@ -2156,5 +2219,5 @@
 		private EntitySet<HiveExperimentPermission> _HiveExperimentPermissions;
 		
-		private EntityRef<Job> _Job;
+		private EntitySet<Job> _Jobs;
 		
     #region Extensibility Method Definitions
@@ -2172,6 +2235,4 @@
     partial void OnOwnerUserIdChanging(System.Guid value);
     partial void OnOwnerUserIdChanged();
-    partial void OnRootJobIdChanging(System.Guid value);
-    partial void OnRootJobIdChanged();
     partial void OnDateCreatedChanging(System.DateTime value);
     partial void OnDateCreatedChanged();
@@ -2185,5 +2246,5 @@
 		{
 			this._HiveExperimentPermissions = new EntitySet<HiveExperimentPermission>(new Action<HiveExperimentPermission>(this.attach_HiveExperimentPermissions), new Action<HiveExperimentPermission>(this.detach_HiveExperimentPermissions));
-			this._Job = default(EntityRef<Job>);
+			this._Jobs = new EntitySet<Job>(new Action<Job>(this.attach_Jobs), new Action<Job>(this.detach_Jobs));
 			OnCreated();
 		}
@@ -2289,28 +2350,4 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_RootJobId", DbType="UniqueIdentifier")]
-		public System.Guid RootJobId
-		{
-			get
-			{
-				return this._RootJobId;
-			}
-			set
-			{
-				if ((this._RootJobId != value))
-				{
-					if (this._Job.HasLoadedOrAssignedValue)
-					{
-						throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
-					}
-					this.OnRootJobIdChanging(value);
-					this.SendPropertyChanging();
-					this._RootJobId = value;
-					this.SendPropertyChanged("RootJobId");
-					this.OnRootJobIdChanged();
-				}
-			}
-		}
-		
 		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DateCreated", DbType="DateTime")]
 		public System.DateTime DateCreated
@@ -2386,19 +2423,14 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Job_HiveExperiment", Storage="_Job", ThisKey="RootJobId", OtherKey="JobId", IsForeignKey=true, DeleteRule="CASCADE")]
-		public Job RootJob
-		{
-			get
-			{
-				return this._Job.Entity;
-			}
-			set
-			{
-				if ((this._Job.Entity != value))
-				{
-					this.SendPropertyChanging();
-					this._Job.Entity = value;
-					this.SendPropertyChanged("RootJob");
-				}
+		[global::System.Data.Linq.Mapping.AssociationAttribute(Name="HiveExperiment_Job", Storage="_Jobs", ThisKey="HiveExperimentId", OtherKey="HiveExperimentId")]
+		public EntitySet<Job> Jobs
+		{
+			get
+			{
+				return this._Jobs;
+			}
+			set
+			{
+				this._Jobs.Assign(value);
 			}
 		}
@@ -2431,4 +2463,16 @@
 		
 		private void detach_HiveExperimentPermissions(HiveExperimentPermission entity)
+		{
+			this.SendPropertyChanging();
+			entity.HiveExperiment = null;
+		}
+		
+		private void attach_Jobs(Job entity)
+		{
+			this.SendPropertyChanging();
+			entity.HiveExperiment = this;
+		}
+		
+		private void detach_Jobs(Job entity)
 		{
 			this.SendPropertyChanging();
Index: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Tools/prepareHiveDatabase.sql
===================================================================
--- branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Tools/prepareHiveDatabase.sql	(revision 5955)
+++ branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Tools/prepareHiveDatabase.sql	(revision 6006)
@@ -55,4 +55,11 @@
 GO
 
+--ALTER TABLE [dbo].[Job]  DROP  CONSTRAINT [HiveExperiment_Job]
+--ALTER TABLE [dbo].[Job]  WITH CHECK ADD CONSTRAINT [HiveExperiment_Job] FOREIGN KEY([HiveExperimentId])
+--REFERENCES [dbo].[HiveExperiment] ([HiveExperimentId])
+--ON UPDATE CASCADE
+--ON DELETE CASCADE
+--GO
+
 ALTER TABLE dbo.Plugin ALTER COLUMN PluginId ADD ROWGUIDCOL;
 ALTER TABLE dbo.Plugin WITH NOCHECK ADD CONSTRAINT [DF_Plugin_PluginId] DEFAULT (newid()) FOR PluginId;
@@ -107,11 +114,13 @@
 -- =============================================
 -- Author:		cneumuel
--- Create date: 11.11.2010
--- Description:	Deletes the root-job of the experiment when experiment is deleted
+-- Create date: 11.04.2011
+-- Description:	Deletes all associated jobs. This cannot be done with cascading delete, 
+--              because the job table defines a INSTEAD OF DELETE trigger itself, which
+--              is not compatible with cascading deletes.
 -- =============================================
-CREATE TRIGGER [dbo].[tr_HiveExperimentDeleteCascade] ON [dbo].[HiveExperiment] FOR DELETE AS 
-SET NOCOUNT ON
-DELETE Job FROM deleted, Job WHERE Job.JobId = deleted.RootJobId
-GO
+ALTER TRIGGER [dbo].[tr_HiveExperimentDeleteCascade] ON [dbo].[HiveExperiment] INSTEAD OF DELETE AS 
+BEGIN
+	DELETE Job FROM Job WHERE Job.HiveExperimentId = HiveExperimentId
+END
 
 SET ANSI_NULLS ON
