org.springframework.orm.hibernate3.support
Class BlobStringType

java.lang.Object
  extended by org.springframework.orm.hibernate3.support.AbstractLobType
      extended by org.springframework.orm.hibernate3.support.BlobStringType
All Implemented Interfaces:
org.hibernate.usertype.UserType

public class BlobStringType
extends AbstractLobType

Hibernate UserType implementation for Strings that get mapped to BLOBs. Retrieves the LobHandler to use from LocalSessionFactoryBean at config time.

This is intended for the (arguably unnatural, but still common) case where character data is stored in a binary LOB. This requires encoding and decoding the characters within this UserType; see the javadoc of the getCharacterEncoding() method.

Can also be defined in generic Hibernate mappings, as DefaultLobCreator will work with most JDBC-compliant database drivers. In this case, the field type does not have to be BLOB: For databases like MySQL and MS SQL Server, any large enough binary type will work.

Since:
1.2.7
Author:
Juergen Hoeller
See Also:
getCharacterEncoding(), LocalSessionFactoryBean.setLobHandler(org.springframework.jdbc.support.lob.LobHandler)

Field Summary
 
Fields inherited from class org.springframework.orm.hibernate3.support.AbstractLobType
logger
 
Constructor Summary
  BlobStringType()
          Constructor used by Hibernate: fetches config-time LobHandler and config-time JTA TransactionManager from LocalSessionFactoryBean.
protected BlobStringType(LobHandler lobHandler, TransactionManager jtaTransactionManager)
          Constructor used for testing: takes an explicit LobHandler and an explicit JTA TransactionManager (can be null).
 
Method Summary
protected  String getCharacterEncoding()
          Determine the character encoding to apply to the BLOB's bytes to turn them into a String.
protected  Object nullSafeGetInternal(ResultSet rs, String[] names, Object owner, LobHandler lobHandler)
          Template method to extract a value from the given result set.
protected  void nullSafeSetInternal(PreparedStatement ps, int index, Object value, LobCreator lobCreator)
          Template method to set the given parameter value on the given statement.
 Class returnedClass()
           
 int[] sqlTypes()
           
 
Methods inherited from class org.springframework.orm.hibernate3.support.AbstractLobType
assemble, deepCopy, disassemble, equals, hashCode, isMutable, nullSafeGet, nullSafeSet, replace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BlobStringType

public BlobStringType()
Constructor used by Hibernate: fetches config-time LobHandler and config-time JTA TransactionManager from LocalSessionFactoryBean.

See Also:
LocalSessionFactoryBean.getConfigTimeLobHandler(), LocalSessionFactoryBean.getConfigTimeTransactionManager()

BlobStringType

protected BlobStringType(LobHandler lobHandler,
                         TransactionManager jtaTransactionManager)
Constructor used for testing: takes an explicit LobHandler and an explicit JTA TransactionManager (can be null).

Method Detail

sqlTypes

public int[] sqlTypes()

returnedClass

public Class returnedClass()

nullSafeGetInternal

protected Object nullSafeGetInternal(ResultSet rs,
                                     String[] names,
                                     Object owner,
                                     LobHandler lobHandler)
                              throws SQLException,
                                     UnsupportedEncodingException
Description copied from class: AbstractLobType
Template method to extract a value from the given result set.

Specified by:
nullSafeGetInternal in class AbstractLobType
Parameters:
rs - the ResultSet to extract from
names - the column names
owner - the containing entity
lobHandler - the LobHandler to use
Returns:
the extracted value
Throws:
SQLException - if thrown by JDBC methods
UnsupportedEncodingException

nullSafeSetInternal

protected void nullSafeSetInternal(PreparedStatement ps,
                                   int index,
                                   Object value,
                                   LobCreator lobCreator)
                            throws SQLException,
                                   UnsupportedEncodingException
Description copied from class: AbstractLobType
Template method to set the given parameter value on the given statement.

Specified by:
nullSafeSetInternal in class AbstractLobType
Parameters:
ps - the PreparedStatement to set on
index - the statement parameter index
value - the value to set
lobCreator - the LobCreator to use
Throws:
SQLException - if thrown by JDBC methods
UnsupportedEncodingException

getCharacterEncoding

protected String getCharacterEncoding()
Determine the character encoding to apply to the BLOB's bytes to turn them into a String.

Default is null, indicating to use the platform default encoding. To be overridden in subclasses for a specific encoding such as "ISO-8859-1" or "UTF-8".

Returns:
the character encoding to use, or null to use the platform default encoding
See Also:
String.String(byte[], String), String.getBytes(String)