-->
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: Other dataType in entity
PostPosted: Wed Aug 19, 2009 8:03 am 
Beginner
Beginner

Joined: Sat Dec 09, 2006 12:02 pm
Posts: 26
Hi,

can anybody tell me how that works? I want to have the Type "bool" in my Entity but i want that this is saved as an integer in the database... Can anybody explain how i have to map this property? (I use fluent Hibernate, but to know how this would be mapped in an hbm-file would also be helpful)


Top
 Profile  
 
 Post subject: Re: Other dataType in entity
PostPosted: Wed Aug 19, 2009 11:20 am 
Beginner
Beginner

Joined: Sat Dec 09, 2006 12:02 pm
Posts: 26
Ha, i got it...

i wrote an own mapper. i want to use a boolean property in my entities, but in the database i want to save an integer (well, actually i dont want it, but the stupid DB2 canot handle bool).

In your entity:
public virtual bool IsActive { get; set; }

in your mapper
Map(x => x.IsActive).CustomType(typeof(BooleanType));

Write the own Type BooleanType
public class BooleanType : IUserType
{
#region IUserType Member
public bool IsMutable
{
get { return false; }
}

public Type ReturnedType
{
get { return typeof(BooleanType); }
}

SqlType baseType = new SqlType(System.Data.DbType.Int16);
public SqlType[] SqlTypes
{
get { return new[] { baseType }; }
}

public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
{
var obj = NHibernateUtil.Int16.NullSafeGet(rs, names[0]);
if (obj == null) return null;

bool bolVal = Convert.ToBoolean(obj);
return bolVal;
}

public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
{
if (value == null)
{
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
}
else
{
bool bolVal = Convert.ToBoolean(value);
((IDataParameter)cmd.Parameters[index]).Value = bolVal ? 1 : 0; // Wenn true, dann eins (true), ansonsten 0 (false)
}

}

public object DeepCopy(object value)
{
if (value == null) return 0;
return value;
}

public object Replace(object original, object target, object owner)
{
return original;
}

public object Assemble(object cached, object owner)
{
return cached;
}

public object Disassemble(object value)
{
return value;
}

public new bool Equals(object x, object y)
{
if (ReferenceEquals(x, y)) return true;

if (x == null || y == null) return false;

return x.Equals(y);
}

public int GetHashCode(object x)
{
return x.GetHashCode();//x == null ? typeof(bool).GetHashCode() + 473 : x.GetHashCode();
}


#endregion

Well, i havent tested the reading yet, but thats a good start.

Hope that helps
Nico


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.