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.  [ 4 posts ] 
Author Message
 Post subject: Guid/UUID to raw(16) field with Oracle
PostPosted: Wed Aug 29, 2007 5:06 am 
Newbie

Joined: Wed Aug 29, 2007 4:59 am
Posts: 1
Hi,
i have a problem with Nhibernate with Oracle DB.
I must mapping a raw(16) identity field.
What is the correct way to map Guid/UUID to raw(16) field with Oracle?

Thanks in advance

Enrico


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 23, 2007 12:05 pm 
Newbie

Joined: Sun Dec 23, 2007 11:34 am
Posts: 2
Hi,

I've solved the issue with few tweaks, I'm sure theses tweaks are weak since I'm not used to the best practices in nhibernate code structure and have done them in hurry:


in ~/NHibernate/Dialect/Oracle9Dialect.cs
Replace
RegisterColumnType(DbType.Guid, "CHAR(38)");
With
RegisterColumnType(DbType.Guid, "RAW(16)");

also have modified ~/NHibernate/Type/GuidType.cs but I'm unsure now if it's required (it helped me handling Guid in oracle and sqlite and others) and that's BAD HACK:

Code:
public override object Get(IDataReader rs, int index)
{
  System.Type type = rs.GetFieldType(index);
  byte[] buffer;
  if(type == typeof(Guid)){
    buffer = ((Guid)rs[index]).ToByteArray();
  } else
    if(type == typeof(byte[])){
      buffer = (byte[]) rs[index];
    } else if (type == typeof(string)) {
      buffer = (new Guid((string)rs[index])).ToByteArray();
    } else {
      return null; // ouch
    }
  return new Guid(buffer);
}

public override void Set(IDbCommand cmd, object value, int index) {
  IDataParameter parm = cmd.Parameters[index] as IDataParameter;
#if SQLITE || MSSQL
         parm.Value = ((Guid)value);
#else
         parm.Value = ((Guid)value).ToByteArray(); // oracle provider won't convert Guid to binary :(
#endif
}


It seems obvious to me that Guid should be mapped as binary by default, but it doesn't seems to be the case in the NHibernate codebase and across drivers implementation.

I don't know if there is better way (using IUserType or such thing?) that does not require to change MapType which should stay implicit when using System.Guid

Any guidance on this topic would help because I'm worried to "maintain" such hacks in the NHibernate codebase and a nicer way should be implemented.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 9:53 am 
Newbie

Joined: Thu Jan 17, 2008 11:45 am
Posts: 9
Location: VA
gauthier - I realize this has been posted for a little while but it really helped us out when trying to deal with the Oracle RAW datatype.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 13, 2008 12:45 am 
Newbie

Joined: Fri Mar 28, 2008 2:11 pm
Posts: 7
Location: Seattle
Here are two suggested solutions using IUserType

http://michaelhanney.com/blog/2008/04/12/nhibernate-oracle-guid-as-primary-key-mapping-custom-iusertype/

http://xmlguy.wordpress.com/2008/04/10/nhibernate-tales-n-mapping-oracle-raw/


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