KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate > support > BlobStringType


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.hibernate.support;
18
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Types JavaDoc;
24
25 import javax.transaction.TransactionManager JavaDoc;
26
27 import org.springframework.jdbc.support.lob.LobCreator;
28 import org.springframework.jdbc.support.lob.LobHandler;
29
30 /**
31  * Hibernate UserType implementation for Strings that get mapped to BLOBs.
32  * Retrieves the LobHandler to use from LocalSessionFactoryBean at config time.
33  *
34  * <p>This is intended for the (arguably unnatural, but still common) case
35  * where character data is stored in a binary LOB. This requires encoding
36  * and decoding the characters within this UserType; see the javadoc of the
37  * <code>getCharacterEncoding()</code> method.
38  *
39  * <p>Can also be defined in generic Hibernate mappings, as DefaultLobCreator will
40  * work with most JDBC-compliant database drivers. In this case, the field type
41  * does not have to be BLOB: For databases like MySQL and MS SQL Server, any
42  * large enough binary type will work.
43  *
44  * @author Juergen Hoeller
45  * @since 1.2.7
46  * @see #getCharacterEncoding()
47  * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#setLobHandler
48  */

49 public class BlobStringType extends AbstractLobType {
50
51     /**
52      * Constructor used by Hibernate: fetches config-time LobHandler and
53      * config-time JTA TransactionManager from LocalSessionFactoryBean.
54      * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#getConfigTimeLobHandler
55      * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#getConfigTimeTransactionManager
56      */

57     public BlobStringType() {
58         super();
59     }
60
61     /**
62      * Constructor used for testing: takes an explicit LobHandler
63      * and an explicit JTA TransactionManager (can be <code>null</code>).
64      */

65     protected BlobStringType(LobHandler lobHandler, TransactionManager JavaDoc jtaTransactionManager) {
66         super(lobHandler, jtaTransactionManager);
67     }
68
69     public int[] sqlTypes() {
70         return new int[] {Types.BLOB};
71     }
72
73     public Class JavaDoc returnedClass() {
74         return String JavaDoc.class;
75     }
76
77     protected Object JavaDoc nullSafeGetInternal(ResultSet JavaDoc rs, int index, LobHandler lobHandler)
78             throws SQLException JavaDoc, UnsupportedEncodingException JavaDoc {
79
80         byte[] bytes = lobHandler.getBlobAsBytes(rs, index);
81         if (bytes != null) {
82             String JavaDoc encoding = getCharacterEncoding();
83             return (encoding != null ? new String JavaDoc(bytes, encoding) : new String JavaDoc(bytes));
84         }
85         else {
86             return null;
87         }
88     }
89
90     protected void nullSafeSetInternal(PreparedStatement JavaDoc ps, int index, Object JavaDoc value, LobCreator lobCreator)
91             throws SQLException JavaDoc, UnsupportedEncodingException JavaDoc {
92
93         if (value != null) {
94             String JavaDoc str = (String JavaDoc) value;
95             String JavaDoc encoding = getCharacterEncoding();
96             byte[] bytes = (encoding != null ? str.getBytes(encoding) : str.getBytes());
97             lobCreator.setBlobAsBytes(ps, index, bytes);
98         }
99         else {
100             lobCreator.setBlobAsBytes(ps, index, null);
101         }
102     }
103
104     /**
105      * Determine the character encoding to apply to the BLOB's bytes
106      * to turn them into a String.
107      * <p>Default is <code>null</code>, indicating to use the platform
108      * default encoding. To be overridden in subclasses for a specific
109      * encoding such as "ISO-8859-1" or "UTF-8".
110      * @return the character encoding to use, or <code>null</code>
111      * to use the platform default encoding
112      * @see String#String(byte[], String)
113      * @see String#getBytes(String)
114      */

115     protected String JavaDoc getCharacterEncoding() {
116         return null;
117     }
118
119 }
120
Popular Tags