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: automatic dirty checking
PostPosted: Fri Jan 30, 2009 5:32 am 
Newbie

Joined: Fri Jan 30, 2009 5:06 am
Posts: 3
Hi

I use NHibernate with a database containing fixed length character data (firstname, lastname...)

I use Windows Presentation Foundation and make databinding with my entities.

The problem is that there are trailing blanks at the end of my character data members.

I try to add .TrimEnd() on the getter or the setter of these properties but the automatic dirty checking
updates automatically the data just after reading them (i.e. update query just after the select query)

Is there a way to prevent this

Thanks


Top
 Profile  
 
 Post subject: Re: automatic dirty checking
PostPosted: Wed May 20, 2009 5:36 am 
Newbie

Joined: Mon May 18, 2009 1:59 pm
Posts: 1
// This IUserType is for trimming the spaces from strings before loading into your objects and database
// To use this you must reference this type, for the properties you want trimmed, in your mapping files for example:
// <property name="FirstName" column="FIRSTNM" type="MyNHibernateUserTypes.StringTrim, MyNHibernateUserTypes"/>

using System;
using System.Data;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;

namespace MyNHibernateUserTypes
{
/// <summary>
/// Trims a string before passing it to the object and database
/// </summary>
public class StringTrim : IUserType
{

public bool IsMutable
{
get { return true; }
}

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

public SqlType[] SqlTypes
{
get { return new[] { NHibernateUtil.String.SqlType }; }
}

public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]);
if (obj == null)
{
return null;
}
else
{
return ((String)obj).Trim();
}
}

public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if ((String)value == String.Empty)
{
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
}
else
{
((IDataParameter)cmd.Parameters[index]).Value = ((String)value).Trim();
}
}

public object DeepCopy(object value)
{
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();
}
}
}


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.