001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.orm.hibernate3.support; 018 019import java.sql.PreparedStatement; 020import java.sql.ResultSet; 021import java.sql.SQLException; 022import java.sql.Types; 023import javax.transaction.TransactionManager; 024 025import org.springframework.jdbc.support.lob.LobCreator; 026import org.springframework.jdbc.support.lob.LobHandler; 027 028/** 029 * Hibernate UserType implementation for Strings that get mapped to CLOBs. 030 * Retrieves the LobHandler to use from LocalSessionFactoryBean at config time. 031 * 032 * <p>Particularly useful for storing Strings with more than 4000 characters in an 033 * Oracle database (only possible via CLOBs), in combination with OracleLobHandler. 034 * 035 * <p>Can also be defined in generic Hibernate mappings, as DefaultLobCreator will 036 * work with most JDBC-compliant database drivers. In this case, the field type 037 * does not have to be CLOB: For databases like MySQL and MS SQL Server, any 038 * large enough character type will work. 039 * 040 * @author Juergen Hoeller 041 * @since 1.2 042 * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#setLobHandler 043 * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x 044 */ 045@Deprecated 046public class ClobStringType extends AbstractLobType { 047 048 /** 049 * Constructor used by Hibernate: fetches config-time LobHandler and 050 * config-time JTA TransactionManager from LocalSessionFactoryBean. 051 * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeLobHandler 052 * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeTransactionManager 053 */ 054 public ClobStringType() { 055 super(); 056 } 057 058 /** 059 * Constructor used for testing: takes an explicit LobHandler 060 * and an explicit JTA TransactionManager (can be {@code null}). 061 */ 062 protected ClobStringType(LobHandler lobHandler, TransactionManager jtaTransactionManager) { 063 super(lobHandler, jtaTransactionManager); 064 } 065 066 @Override 067 public int[] sqlTypes() { 068 return new int[] {Types.CLOB}; 069 } 070 071 @Override 072 public Class<?> returnedClass() { 073 return String.class; 074 } 075 076 @Override 077 protected Object nullSafeGetInternal( 078 ResultSet rs, String[] names, Object owner, LobHandler lobHandler) 079 throws SQLException { 080 081 return lobHandler.getClobAsString(rs, names[0]); 082 } 083 084 @Override 085 protected void nullSafeSetInternal( 086 PreparedStatement ps, int index, Object value, LobCreator lobCreator) 087 throws SQLException { 088 089 lobCreator.setClobAsString(ps, index, (String) value); 090 } 091 092}