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: Assign Value to Identity Column
PostPosted: Thu Feb 07, 2008 6:38 am 
Newbie

Joined: Thu Feb 07, 2008 6:28 am
Posts: 1
I have an object named PCSPrice with a property of Identifier and contains the related XML in the hbm file:

Code:
    <id name="Identifier" column="Identifier" type="Guid">
      <generator class="guid"/>
    </id>


What I want to do is assign a Guid in code like:

Code:
Guid identifier = Guid.NewGuid();

pcsPrice.Identifier = identifier;
anotherObject.Identifier = identifier;

//Save here



Right now whenever I call the above code, NHibernate creates a new Guid for the Identifier field - even though I've already assigned one. I've played around with the unsaved-value attribute but with no luck. How do I assign a Guid to an Identifier field without it being overwritten by NHibernate?

Thanks,

Kyle


Top
 Profile  
 
 Post subject: Assign Value to Identity Column
PostPosted: Thu Feb 07, 2008 9:24 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

If you set the generator to 'assigned', then your application will be responsible for setting the identifier, rather than NHibernate.

Code:
    <id name="Identifier" column="Identifier" type="Guid">
      <generator class="assigned"/>
    </id>


Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 07, 2008 4:10 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
If you want to be able to use both assigned and native id generation try something like this (although it's hard-coded to use type long for ids, and 0 as the unsaved-value, so you might want to mess around with it):

Code:
public class IdGenerator : NHibernate.Id.IdentityGenerator, NHibernate.Id.IIdentifierGenerator
{
  public new object Generate(NHibernate.Engine.ISessionImplementor session, object obj)
        {

            IEntityPersister persister = session.GetEntityPersister(obj);
  // Determine if an ID has been assigned.
            object id = persister.GetIdentifier(obj);
            if (id != null && (long)id != 0) return id;
            return IdentifierGeneratorFactory.IdentityColumnIndicator;
        }
}


In your mapping file:
Code:
<class name="TestClass" table="test_class">
      <id name="Id" column="id" type="long">
         <generator class="Namespace.IdGenerator, AssemblyName" />
      </id>
...


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.