KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2005 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.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Types JavaDoc;
23 import java.util.Arrays 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 byte arrays that get mapped to BLOBs.
32  * Retrieves the LobHandler to use from LocalSessionFactoryBean at config time.
33  *
34  * <p>Can also be defined in generic Hibernate mappings, as DefaultLobCreator will
35  * work with most JDBC-compliant database drivers. In this case, the field type
36  * does not have to be BLOB: For databases like MySQL and MS SQL Server, any
37  * large enough binary type will work.
38  *
39  * @author Juergen Hoeller
40  * @since 1.1
41  * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#setLobHandler
42  */

43 public class BlobByteArrayType extends AbstractLobType {
44
45     /**
46      * Constructor used by Hibernate: fetches config-time LobHandler and
47      * config-time JTA TransactionManager from LocalSessionFactoryBean.
48      * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#getConfigTimeLobHandler
49      * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#getConfigTimeTransactionManager
50      */

51     public BlobByteArrayType() {
52         super();
53     }
54
55     /**
56      * Constructor used for testing: takes an explicit LobHandler
57      * and an explicit JTA TransactionManager (can be <code>null</code>).
58      */

59     protected BlobByteArrayType(LobHandler lobHandler, TransactionManager JavaDoc jtaTransactionManager) {
60         super(lobHandler, jtaTransactionManager);
61     }
62
63     public int[] sqlTypes() {
64         return new int[] {Types.BLOB};
65     }
66
67     public Class JavaDoc returnedClass() {
68         return byte[].class;
69     }
70
71     public boolean isMutable() {
72         return true;
73     }
74
75     public boolean equals(Object JavaDoc x, Object JavaDoc y) {
76         return (x == y) ||
77                 (x instanceof byte[] && y instanceof byte[] && Arrays.equals((byte[]) x, (byte[]) y));
78     }
79
80     public Object JavaDoc deepCopy(Object JavaDoc value) {
81         if (value == null) {
82             return null;
83         }
84         byte[] original = (byte[]) value;
85         byte[] copy = new byte[original.length];
86         System.arraycopy(original, 0, copy, 0, original.length);
87         return copy;
88     }
89
90     protected Object JavaDoc nullSafeGetInternal(ResultSet JavaDoc rs, int index, LobHandler lobHandler)
91             throws SQLException JavaDoc {
92
93         return lobHandler.getBlobAsBytes(rs, index);
94     }
95
96     protected void nullSafeSetInternal(PreparedStatement JavaDoc ps, int index, Object JavaDoc value, LobCreator lobCreator)
97             throws SQLException JavaDoc {
98         
99         lobCreator.setBlobAsBytes(ps, index, (byte[]) value);
100     }
101
102 }
103
Popular Tags