-->
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.  [ 19 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Need help for PK-FK association through CHAR
PostPosted: Thu May 01, 2008 12:08 pm 
Newbie

Joined: Sat Apr 26, 2008 10:45 pm
Posts: 4
Hi, thanks for all the useful posting.

Can any one tell using the TrimmedString will solve my problem posted with subject "Hibernate Collection Fetch problem with fixed length CHAR"?

Please be kind enough to look into my post, not a single person has replied till now.

Anyway I will take the TrimmedString sourcecode and try.


Top
 Profile  
 
 Post subject: It works
PostPosted: Fri May 02, 2008 7:03 am 
Newbie

Joined: Sat Apr 26, 2008 10:45 pm
Posts: 4
Thanks a lot for providing such useful casses.
It works for the problem I mentioned in the previous post. But in my opinion hibernate should handle this internally.


Top
 Profile  
 
 Post subject: C# TrimmedString Class
PostPosted: Tue Jul 08, 2008 8:04 am 
Newbie

Joined: Tue Jul 08, 2008 7:41 am
Posts: 1
I use Nhibernate, C# and DB/2 and I had the same problem. Couldn't find any C# code so I followed the example in NHibernate.DomainModel.DoubleStringType and that's what I came up with (however, I take no responsibility for the code), but it works so far.

Code:
using System;
using System.Data;
using NHibernate;

namespace SomeNamespace
{
    public class TrimmedString: NHibernate.UserTypes.IUserType
    {
        public TrimmedString()
        {
        }

        public new bool Equals(object x, object y)
        {
            if (x == y) return true;
            if (x == null || y == null) return false;
            return x.Equals(y);
        }

        public object DeepCopy(object obj)
      {
            if (obj == null) return null;
            return new String(((string)obj).ToCharArray());       
        }

        public int GetHashCode(object obj)
        {
            return obj.GetHashCode();
        }

        public bool IsMutable
        {
            get { return false; }
        }

        public object NullSafeGet(
            System.Data.IDataReader rs, string[] names, object owner)
        {
         string str = (string) NHibernateUtil.String.NullSafeGet(rs, names[0]);
            if (str == null)
                return null;
            else
                return str.[color=red]TrimEnd()[/color];
        }

        public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
        {
            NHibernateUtil.String.NullSafeSet(cmd, value, index);
        }

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

        public global::NHibernate.SqlTypes.SqlType[] SqlTypes
        {
            get
            {
                global::NHibernate.SqlTypes.SqlType[] types = new global::NHibernate.SqlTypes.SqlType[1];
                types[0] = new global::NHibernate.SqlTypes.SqlType(DbType.String);
                return types;
            }
        }

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

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

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


Hope it helps...

_________________
Ivo Iliev (some programmer)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2008 12:03 pm 
Newbie

Joined: Fri Oct 10, 2008 11:17 am
Posts: 9
I'm trying to get this to work with javax.persistence annotations instead. Seems reasonably easy ok so far.

How do you change ALL strings to be passed through a special type? For example, unless Hibernate is specifically told (via scalars, objects) it will convert all char(n) fields in informix to a single character which results in a lot of data-loss.

TrimmedString code is as above but with these extra functions...

public Object assemble(Serializable cached, Object owner) {
return deepCopy(cached);
}

public Serializable disassemble(Object arg0) throws HibernateException {
return (Serializable) deepCopy(arg0);
}

public Object replace(Object original, Object target, Object owner) {
return deepCopy(original);
}

public int hashCode(Object arg0) throws HibernateException {
return arg0.hashCode();
}

----
My entity class is like this:

@Entity
@Table(name = "dropme")
@TypeDef(name = "TrimmedString", typeClass = com.intercity.db.controller.TrimmedString.class)
public class Dropme {

private static final long serialVersionUID = 1L;
@Id
@Column(name = "handset_no", nullable = false)
@Type(type="TrimmedString")
private String handset_no;

public Dropme() {
}

public String getHandset_no() {
return handset_no;
}

public void setHandset_no(String handset_no) {
this.handset_no = handset_no;
}
}

----
I haven't had to define a typedef outside in either a hbm.xml file or the persistence.xml file, which was nice.

Now, how do I override the "Character" to always cast as a String? Any ideas?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page Previous  1, 2

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.