-->
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.  [ 2 posts ] 
Author Message
 Post subject: Trying to save object, overriding guid which been set
PostPosted: Wed Jan 16, 2008 7:15 am 
Newbie

Joined: Wed Jan 16, 2008 6:41 am
Posts: 1
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;
}

Classes
Code:
[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; }
   
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 16, 2008 11:51 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
My advice would be, set the id always to "assigned", and then, within your code, use a simple utility that generates a 32-char unique number.

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.