Hi,
I have a problem when trying to save an object which containing a collection of anoter object (CircuitBreaker containing a list of ScheduleTask). I'm setting the value for property id for CircuitBreaker but NHibernate overrids it.
Se below code:
Code:
CircuitBreaker cb = new CircuitBreaker();
cb.Id = Guid.NewGuid();
cb.Name = "testar";
cb.CircuitId = 999;
cb.BreakerType = Breaker.Schedule;
ScheduleTask task1 = new ScheduleTask();
//task1.Id = Guid.NewGuid();
task1.CircuitBreakerId = cb.Id;
task1.Name = "Mjuk";
task1.StartHour = 12;
task1.Startminutes = 0;
task1.DurationInHours = 2;
task1.DurationInMinutes = 22;
CircuitBreakerService service = new CircuitBreakerService();
CircuitBreaker newCB = service.AddCircuitBreaker(cb);
Above, when newCB has a new id. I would like to set the id:s myself, and if not, NHibernate should set it for me. How can I get this to work properly?
I'm using VS 2008 and .NET 3.5.
Best regards
Carl
Hibernate version: 1.2.1
Mapping documents:<class name="CircuitBreaker, Business" table="CircuitBreaker" lazy="false">
<id name="Id" column="Id" type="guid" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="guid" />
</id>
<bag name="ScheduleTasks" inverse="true" lazy="false" cascade="all-delete-orphan">
<key column="Id"/>
<one-to-many class="ScheduleTask, Business"/>
</bag>
<property name="Name" column="Name" type="System.String" />
<property name="CircuitId" column="CircuitId" type="System.Int32" />
<property name="BreakerType" column="BreakerType" type="Breaker,Business"></property>
</class>
<class name="ScheduleTask, Business" table="ScheduleTask" lazy="false">
<id name="Id" column="Id" type="guid" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="guid" />
</id>
<property name="Name" column="Name" type="System.String" />
<property name="CircuitBreakerId" column="CircuitBreakerId" type="System.Guid" />
<property name="StartHour" column="StartHour" type="System.Int32" />
<property name="Startminutes" column="Startminutes" type="System.Int32" />
<property name="DurationInHours" column="DurationInHours" type="System.Int32" />
<property name="DurationInMinutes" column="DurationInMinutes" type="System.Int32" />
</class>
Name and version of the database you are using:MSSQL 2005
Code that saves the objects:ITransaction tx = null;
try
{
tx = Session.BeginTransaction();
Session.Save(cb);
tx.Commit();
}
catch (Exception e)
{
if (tx != null)
tx.Rollback();
throw;
}
ClassesCode:
[Serializable]
public class CircuitBreaker
{
public CircuitBreaker()
{
ScheduleTasks = new List<ScheduleTask>();
}
public Guid Id { get; set; }
public string Name { get; set; }
public int? CircuitId { get; set; }
public Breaker BreakerType { get; set; }
public IList<ScheduleTask> ScheduleTasks { get; set; }
}
[Serializable]
public class ScheduleTask
{
public Guid Id { get; set; }
public Guid CircuitBreakerId { get; set; }
public string Name { get; set; }
public int StartHour { get; set; }
public int Startminutes { get; set; }
public int DurationInHours { get; set; }
public int DurationInMinutes { get; set; }
}