-->
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.  [ 11 posts ] 
Author Message
 Post subject: How to persist a String with length>255 in MySQL?
PostPosted: Wed Dec 17, 2003 6:45 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
Hibernate wants to use Varchar(255) as the type to persist my strings.. but I have a field where the string may be greater than 255 in length. I've read that MySQL can use mediumBlob's to store this kind of stuff, but how do I convert back and forth? I want my POJO to have String as its property type and I just want the database to store it as a medium blob.

Thanks,
David


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 8:26 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
If I remember correctly, MySQL has TEXT type which can hold large strings. TEXT fields do not require any special conversion - like VARCHAR columns they can be used in SELECTs and UPDATEs.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 8:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Hibernate wants to use Varchar(255) as the type to persist my strings


You can change that using the length or sql-type attributes...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 8:39 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
http://www.mysql.com/documentation/mysq ... ring_types

Quote:
Values in VARCHAR columns are variable-length strings. You can declare a VARCHAR column to be any length between 1 and 255, just as for CHAR columns


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 8:46 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
I ended up using my own datatype similar to what has been done with String->Clob in Oracle. Source for anyone interested:

Code:
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;

public class StringBlobType implements UserType {

  public int[] sqlTypes()
  {
      return new int[] { Types.BLOB };
  }

  public Class returnedClass()
  {
      return String.class;
  }

  public boolean equals(Object x, Object y)
  {
      return (x == y)
          || (x != null
              && y != null
              && (x.equals(y)));
  }

  public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
      throws HibernateException, SQLException
  {
      Blob blob = rs.getBlob(names[0]);
      byte[] array = blob.getBytes(1, (int)blob.length());
      return new String(array);
  }

  public void nullSafeSet(PreparedStatement st, Object value, int index)
      throws HibernateException, SQLException
  {
      st.setBlob(index, Hibernate.createBlob(((String)value).getBytes()));
  }

  public Object deepCopy(Object value)
  {
      if (value == null) return null;
      return new String((String) value);
  }

  public boolean isMutable()
  {
      return false;
  }

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 8:50 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Hey, hey!
User type is a great thing but I believe it is not needed here. Try setting sql-type of the property (and changing column type) to "text" first.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 9:02 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
ahh you are right it did work! Well now I know it can be done either way =))

I was reading the difference between blob/text and it seems that the only difference is if I do comparisons on the data within mysql.. text is non case sensitive whereas blob IS case sensitive. I wonder if that would ever cause a problem??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 9:08 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
TEXT behaves like VARCHAR column. If you search for user WHERE username='test' it will find 'TEST', 'Test', 'tESt' etc.

Again - you will encounter this case-insensitive behavior for VARCHAR columns anyway. So in this aspect TEXT and VARCHAR do not differ.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2003 12:57 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
ahh ok thanks =)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 4:26 am 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
Why have you tried to fall back to a BLOB - doesn't MySQL support CLOBs? I've already wondered though whether CLOBs really add that much advantage over BLOBs: After all, they just do character set conversion.

Juergen


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 1:43 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
I didnt think they did.. and I couldnt find anything about clob support on mysql's webpage


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