Thread:
 method 1 and mysql.. 
 hooverphonique   02 Dec 2004, 09:38 
 Re: method 1 and mysql.. 
 hooverphonique   02 Dec 2004, 09:57 
 Re: method 1 and mysql.. 
 monoxxx   10 Aug 2005, 22:41 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: monoxxx (10 Aug 2005, 22:41) Replies: 0, Views: 37171
Subject: Re: method 1 and mysql..
POST QUESTIONS ON THE FORUM! COMMENTS HERE SHOULD ADD VALUE TO THE 
PAGE!On 02 Dec 2004 09:57, hooverphonique wrote:

>On 02 Dec 2004 09:38, hooverphonique wrote:

>>I can confirm that the first method (using Clob#getSubString) does
>NOT
>>work for MySQL, since the Hibernate ClobImpl class does not allow
>>extraction of data using getSubString, which is exactly the method
>>MySQL uses to get to the character data

>Using method 1 (UserType implementation) with the following code in
>place of nullSafeSet and nullSafeGet works for me for both reading
and 
>writing CLOBs..

>	public Object nullSafeGet(ResultSet rs, String[] names,
>Object owner) throws HibernateException, SQLException {
>		Reader reader = rs.getCharacterStream(names[0]);
>		if (reader == null)	return null;

>		StringBuffer sb = new StringBuffer();
>		try {
>			char[] charbuf = new char[4096];
>			for (int i = reader.read(charbuf); i >
0; i 
>= reader.read(charbuf))	{
>				sb.append(charbuf, 0, i);
>			}
>		}
>		catch (IOException e) {
>			throw new SQLException( e.getMessage
() );
>		}
>		return sb.toString();
>	}

>	public void nullSafeSet(PreparedStatement st, Object value,
>int index) throws HibernateException, SQLException {
>		if (value != null) {
>			StringReader r = new StringReader(
(String)
>value );
>			st.setCharacterStream( index, r,
((String)
>value).length() );
>		} else {
>			st.setNull(index, sqlTypes()[0]);
>		}
>	}


public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
        throws HibernateException, SQLException
    {
        Clob clob = rs.getClob(names[0]);
        return clob.getSubString(1, (int) clob.length());
    }

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

Above two method I can't insert the data into Oracle 9i database;

But When I change below this two method(Use characterStream to char[])
It worked !!

That's amazing !!

Great Thank You  hooverphonique 

public Object nullSafeGet(ResultSet rs, String[] names, 
Object owner) throws HibernateException, SQLException {
		Reader reader = rs.getCharacterStream(names[0]);
		if (reader == null)	return null;

		StringBuffer sb = new StringBuffer();
		try {
			char[] charbuf = new char[4096];
			for (int i = reader.read(charbuf); i > 
0; i 
= reader.read(charbuf))	{
				sb.append(charbuf, 0, i);
			}
		}
		catch (IOException e) {
			throw new SQLException( e.getMessage
() );
		}
		return sb.toString();
	}

	public void nullSafeSet(PreparedStatement st, Object value, 
int index) throws HibernateException, SQLException {
		if (value != null) {
			StringReader r = new StringReader( 
(String)
value );
			st.setCharacterStream( index, r, 
((String)
value).length() );
		} else {
			st.setNull(index, sqlTypes()[0]);
		}
	}
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]