« 日本Androidの会 | メイン | Android - VIEW_ACTIONのIntentでStreet Viewを開く »

Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい

自分ではカラムの型に固定長文字列を使った事がなかったので、このような問題に遭遇したことがありませんでした。解決方法の例としては、アプリケーションのコードで毎回やる方法もありますが、ユーザー型(UserType)を使う方法とかもあります。ユーザー型の場合は、以下のような感じのクラスを作ります。

package example.usertype;

public class RightPadUserType implements UserType, ParameterizedType {
  public int[] sqlTypes() {
    return new int[] { Types.VARCHAR };
  }
  public Class returnedClass() {
    return String.class;
  }
  public boolean equals(Object x, Object y) throws HibernateException {
    return EqualsHelper.equals(x, y);
  }
  public int hashCode(Object x) throws HibernateException {
    return x.hashCode();
  }
  public Object assemble(Serializable cached, Object owner)
      throws HibernateException {
    return cached;
  }
  ・・・(省略)
  public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
      throws HibernateException, SQLException {
    String value = rs.getString(names[0]);
    if(value==null)
      return "";
    return value;
  }
  
  public void nullSafeSet(PreparedStatement st, Object value, int index)
      throws HibernateException, SQLException {
    if (value == null) {
      st.setNull(index, Types.NULL);
    } else {
      st.setString(index, StringUtils.rightPad((String)value, length));
    }
  }
  // カラム長
  private int length = 0;
  public void setParameterValues(Properties properties) {
    if(properties.get("length") != null)
      length = Integer.parseInt((String)properties.get("length"));
  }
}

trimしてもいいのですが、trimだと検索時にindexが効かず遅くなりそうなので、padする方がよいような気がします。ただ、そうすると、カラム長を教えてあげる必要があります(※1)。以下はhbmで書いた場合の例です。

<hibernate-mapping>
 <class name="example.Job" table="Jobs">
・・・
    <property name="name">
      <type name="example.usertype.RightPadUserType">
       <param name="length">10</param> ※1
      </type>
    </property>
・・・
  </class>
</hibernate-mapping>

自動生成する方法を作って工夫しないと面倒そうです。あまりいい方法ではないかもしれません。


トラックバック

このエントリーのトラックバックURL:
http://www.grandnature.net/bin/mt-tb.cgi/86

コメント (69)

That is really fascinating, You might be an excessively specialist blogger. I’ve joined your feed and stay up for searching for more of your excellent post. Also, I’ve shared your website in my social networks!

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

As soon as I identified this web site I went on reddit to share some with the enjoy with them.

Its my destiny to go to see at this webpage and find out my required piece of writing along with video demonstration, that’s YouTube video and its also in quality.

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

It is possible to certainly see your excitement inside the pieces you write. The world hopes for much more passionate bloggers like you who aren’t afraid to say how they think. Always follow your heart.

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

hi!,I like your writing so much! share we keep in touch more approximately your post on AOL? I require an expert in this house to unravel my problem. May be that's you! Having a look forward to look you.

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

Thanks for all of your hard work on this blog. My mom takes pleasure in going through investigation and it is obvious why. A number of us notice all regarding the dynamic ways you convey invaluable techniques through the web blog and as well cause contribution from other ones on this subject matter plus our girl has always been becoming educated a lot of things. Take advantage of the rest of the year. You're the one carrying out a superb job.

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

here:

Hey there! Great post! But the website is always loading slowly.

site:

Hi there! Cool post! But this webpage has been loading pretty slowly.

runescape max hit runescape gold runescape combat level calculator rune scape runescape item ids rs money guide runescape runescape experience chart runescape accounts runescape ranging calculator rs accounts for sale alching calculator runescape accounts for sale runescape forums runescape forum avatar runescape gold runescape melee gear runescape cheats runescape items for sale cheap runescape item id magic calculator runescape runescape item ids high alch runescape high alch prices runescape prayer calculator runescape experience chart runescape level table zybez forum runescape range leveling guide alch runescape high alch runescape calculators runescape calculators

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

Excellent job, I was doing a google search and your website came up for homes for sale in Altamonte Springs, FL but anyway, I have enjoyed reading it, keep it up!

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

I got what you specify, thanks for swing up. Woh I am gladsome to regain this website finished google. Thanks For Share Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

I got what you wish, thanks for swing up. Woh I am cheerful to perceive this website through google. Thanks For Share Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

Sites we Like......

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

Fantastic web site. A lot of useful info here. I am sending it to several friends ans also sharing in delicious. And naturally, thanks for your sweat!

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

Definitely this blog is very informative and neatly designed. Really you've motivated me strongly in my new project by this article. As a constant reader of your blog I want to tell you that your writing skills are superb.

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

Hello! Great post! But the site is still loading slowly.

Coach is also known for its great designer bags and other accessories. You can get Coach designer bags and other Coach accessories at a great discount at a Coach outlet. Apart from the normal discount that is available in Coach outlets you can get more exciting discounts with Coach coupons. So if you are planning to buy a Coach designer handbag make sure you have a look at what’s available and grab yourself a really good deal on your purchase.

Websites worth visiting...

Hello there! Good post! But the webpage is still loading very slowly.

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was searching for! .

Whats up this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I'm starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience. Any help would be enormously appreciated!

An unputdownable communication is couturier account. I suppose that you should create much on this issue, it power not be a preconception someone but mostly fill are not enough to verbalize on specified topics. To the succeeding. Cheers like your Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

An interesting word is designer account. I think that you should create more on this content, it mightiness not be a prejudice master but generally people are not sufficiency to speak on much topics. To the next. Cheers like your Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

Wonderful goods from you, man. Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) I've understand your stuff previous to and you are just too fantastic. I actually like what you have acquired here, certainly like what you are saying and the way in which you say it. You make it entertaining and you still care for to keep it wise. I can not wait to read far more from you. This is really a wonderful Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) informations.

Fantastic goods from you, man. Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) I have understand your stuff previous to and you are just too fantastic. I really like what you've acquired here, certainly like what you're saying and the way in which you say it. You make it entertaining and you still take care of to keep it wise. I cant wait to read far more from you. This is actually a wonderful Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) informations.

hyip:

Fantastic goods from you, man. Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) I have understand your stuff previous to and you are just extremely excellent. I actually like what you've acquired here, certainly like what you are saying and the way in which you say it. You make it enjoyable and you still take care of to keep it wise. I cant wait to read far more from you. This is actually a great Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) informations.

hyip:

Fantastic goods from you, man. Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) I've understand your stuff previous to and you are just too fantastic. I actually like what you've acquired here, certainly like what you are saying and the way in which you say it. You make it enjoyable and you still take care of to keep it wise. I cant wait to read much more from you. This is actually a wonderful Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) informations.

This page appears to get a good ammount of visitors. How do you advertise it? It offers a nice individual twist on things. I guess having something authentic or substantial to talk about is the most important thing.

I got what you will, thanks for putting up. Woh I am gladsome to pronounce this website finished google. Thanks For Share Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

I got what you impart, thanks for putting up. Woh I am happy to label this website through google. Thanks For Share Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

I got what you will, thanks for putting up. Woh I am willing to find this website through google. Thanks For Share Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

I got what you impart, thanks for swing up. Woh I am glad to bump this website finished google. Thanks For Share Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

I got what you intend, thanks for swing up. Woh I am gladsome to exploit this website finished google. Thanks For Share Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

I got what you destine, thanks for swing up. Woh I am pleased to reason this website through google. Thanks For Share Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

I got what you signify, thanks for swing up. Woh I am glad to regain this website finished google. Thanks For Share Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

An fascinating communication is couturier interpret. I conceive that you should pen author on this message, it strength not be a bias someone but mostly fill are not enough to speak on much topics. To the succeeding. Cheers like your Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature).

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

I like Your Article about Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) Perfect just what I was looking for! .

Magnificent goods from you, man. Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) I have understand your stuff previous to and you are just too excellent. I actually like what you've acquired here, certainly like what you are saying and the way in which you say it. You make it enjoyable and you still take care of to keep it sensible. I cant wait to read much more from you. This is actually a great Hibernate - 固定長文字列(CHARなど)のカラムに対して半角スペースでpaddingしたい (GrandNature) informations.

コメントを投稿

About

2008年09月16日 12:44に投稿されたエントリーのページです。

ひとつ前の投稿は「日本Androidの会」です。

次の投稿は「Android - VIEW_ACTIONのIntentでStreet Viewを開く」です。

他にも多くのエントリーがあります。メインページアーカイブページも見てください。