01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.orm.hibernate3.support;
18:
19: import java.sql.PreparedStatement;
20: import java.sql.ResultSet;
21: import java.sql.SQLException;
22: import java.sql.Types;
23:
24: import javax.transaction.TransactionManager;
25:
26: import org.springframework.jdbc.support.lob.LobCreator;
27: import org.springframework.jdbc.support.lob.LobHandler;
28:
29: /**
30: * Hibernate UserType implementation for Strings that get mapped to CLOBs.
31: * Retrieves the LobHandler to use from LocalSessionFactoryBean at config time.
32: *
33: * <p>Particularly useful for storing Strings with more than 4000 characters in an
34: * Oracle database (only possible via CLOBs), in combination with OracleLobHandler.
35: *
36: * <p>Can also be defined in generic Hibernate mappings, as DefaultLobCreator will
37: * work with most JDBC-compliant database drivers. In this case, the field type
38: * does not have to be CLOB: For databases like MySQL and MS SQL Server, any
39: * large enough character type will work.
40: *
41: * @author Juergen Hoeller
42: * @since 1.2
43: * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#setLobHandler
44: */
45: public class ClobStringType extends AbstractLobType {
46:
47: /**
48: * Constructor used by Hibernate: fetches config-time LobHandler and
49: * config-time JTA TransactionManager from LocalSessionFactoryBean.
50: * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeLobHandler
51: * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeTransactionManager
52: */
53: public ClobStringType() {
54: super ();
55: }
56:
57: /**
58: * Constructor used for testing: takes an explicit LobHandler
59: * and an explicit JTA TransactionManager (can be <code>null</code>).
60: */
61: protected ClobStringType(LobHandler lobHandler,
62: TransactionManager jtaTransactionManager) {
63: super (lobHandler, jtaTransactionManager);
64: }
65:
66: public int[] sqlTypes() {
67: return new int[] { Types.CLOB };
68: }
69:
70: public Class returnedClass() {
71: return String.class;
72: }
73:
74: protected Object nullSafeGetInternal(ResultSet rs, String[] names,
75: Object owner, LobHandler lobHandler) throws SQLException {
76:
77: return lobHandler.getClobAsString(rs, names[0]);
78: }
79:
80: protected void nullSafeSetInternal(PreparedStatement ps, int index,
81: Object value, LobCreator lobCreator) throws SQLException {
82:
83: lobCreator.setClobAsString(ps, index, (String) value);
84: }
85:
86: }
|