-->
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.  [ 5 posts ] 
Author Message
 Post subject: Setting Id Column
PostPosted: Sun Jul 05, 2009 7:48 am 
Newbie

Joined: Sat Mar 21, 2009 5:22 am
Posts: 10
Hi,

I have a class called Animal which contains the property:
public virtual Guid Id { get; set; }

My mapping for this field is:
<id name="Id">
<generator class="guid" />
</id>

If I create a new instance of the class a Guid is generated for me and the item successfully persists to the DB.

However, I need to be able to also specify the Guid for the new record in code (generator class would then only be used if Id was equal to null). For example:
Animal animal = new Animal() { Id = new Guid("2c1be905-d12c-49c7-bd0a-35427ac7cbb1") };

The problem is that the generator class still creates a new Guid rather than using the Guid I supply.
Is there an easy way to achieve this?

Thanks heaps in advance.


Top
 Profile  
 
 Post subject: Re: Setting Id Column
PostPosted: Mon Jul 06, 2009 5:59 am 
Beginner
Beginner

Joined: Fri Feb 27, 2009 6:07 am
Posts: 38
Location: Moscow,Russia
Code:
<id name="Id">
<generator class="assigned" />
</id>


Top
 Profile  
 
 Post subject: Re: Setting Id Column
PostPosted: Mon Jul 06, 2009 10:01 am 
Newbie

Joined: Sat Mar 21, 2009 5:22 am
Posts: 10
Thanks for the reply.

But I need the Id to be auto generated for me if it is null. Additionally I need to be able to assign the value myself.

Can this be done?

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Setting Id Column
PostPosted: Thu Jul 09, 2009 5:37 am 
Newbie

Joined: Sat Mar 21, 2009 5:22 am
Posts: 10
Can someone please help!


Top
 Profile  
 
 Post subject: Re: Setting Id Column
PostPosted: Thu Jul 09, 2009 7:01 am 
Newbie

Joined: Sat Mar 21, 2009 5:22 am
Posts: 10
I have a solution. I need to create my own custom Id generator. Something like this

public class GuidGenerator : IIdentifierGenerator
{
#region IIdentifierGenerator Members

/// <summary>
/// Generate a new <see cref="Guid"/> for the identifier.
/// </summary>
/// <param name="session">The <see cref="ISessionImplementor"/> this id is being generated in.</param>
/// <param name="obj">The entity for which the id is being generated.</param>
/// <returns>The new identifier as a <see cref="Guid"/>.</returns>
public object Generate(ISessionImplementor session, object obj)
{
Guid value = (Guid)obj.GetType().GetProperty("Id").GetValue (obj,null);
if (value.Equals(Guid.Empty))
return Guid.NewGuid();
else
return value;
}

#endregion
}



Enjoy!


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