-->
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: Creating a custom generator for a custom Id datatype?
PostPosted: Mon May 29, 2006 4:55 pm 
Beginner
Beginner

Joined: Tue Jul 19, 2005 11:21 am
Posts: 23
Location: erie, pa
I'm trying to save a domain object with a custom type for its Id, a class called Id that wraps an Int32. I'm trying to use the native generator but when I try to save a new object, I get the following exception...

InvalidCastException: Invalid cast from 'System.Decimal' to 'Project.DataTypes.Id'.]

This is on line 93 of IdentifierGeneratorFactory.cs:

return Convert.ChangeType( identityValue, clazz );

Note that I am working with the most recent NHibernate trunk code updated from the SVN repository as of this morning. My DB engine is MS SqlExpress and hence the reason its getting a decimal for the Identity. I tried adding an explicit and implicit cast operation on my class from decimal to Id but that didn't change anything. I used Reflector to look at the source code for Convert.ChangeType and that looks like its all based on the IConvertable interface and so I'm kinda stuck there.

I'm guessing that I will need to make a custom generator for my custom type. I would still like to use the database generated identifier though. I see the interface IIdentifierGenerator which I assume I need implement but this is where I'm lost and kinda wading through the code trying to figure out what to do next.

<id name="Id" column="ID" unsaved-value="0" type="Project.Persistence.NHibernateTypes.IdType, Project.Persistence">
<generator class="native" />
</id>

I know I can replace "native" with a custom type but writing this type is where I'm struggling. Some pointers would be helpful if anyone has done this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 29, 2006 11:03 pm 
Beginner
Beginner

Joined: Tue Jul 19, 2005 11:21 am
Posts: 23
Location: erie, pa
Looking into the NHibernate code a little further, I think I have found a bug, reported as http://jira.nhibernate.org/browse/NH-621 and added a patch.


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.