View Javadoc
1   /*
2    * Copyright 2002-2012 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.hibernate3.support;
18  
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  import java.sql.Types;
23  import java.util.Arrays;
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 byte arrays that get mapped to BLOBs.
31   * Retrieves the LobHandler to use from LocalSessionFactoryBean at config time.
32   *
33   * <p>Can also be defined in generic Hibernate mappings, as DefaultLobCreator will
34   * work with most JDBC-compliant database drivers. In this case, the field type
35   * does not have to be BLOB: For databases like MySQL and MS SQL Server, any
36   * large enough binary type will work.
37   *
38   * @author Juergen Hoeller
39   * @since 1.2
40   * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#setLobHandler
41   */
42  public class BlobByteArrayType extends AbstractLobType  {
43  
44  	/**
45  	 * Constructor used by Hibernate: fetches config-time LobHandler and
46  	 * config-time JTA TransactionManager from LocalSessionFactoryBean.
47  	 * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeLobHandler
48  	 * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#getConfigTimeTransactionManager
49  	 */
50  	public BlobByteArrayType() {
51  		super();
52  	}
53  
54  	/**
55  	 * Constructor used for testing: takes an explicit LobHandler
56  	 * and an explicit JTA TransactionManager (can be {@code null}).
57  	 */
58  	protected BlobByteArrayType(LobHandler lobHandler, TransactionManager jtaTransactionManager) {
59  		super(lobHandler, jtaTransactionManager);
60  	}
61  
62  	@Override
63  	public int[] sqlTypes() {
64  		return new int[] {Types.BLOB};
65  	}
66  
67  	@Override
68  	public Class<?> returnedClass() {
69  		return byte[].class;
70  	}
71  
72  	@Override
73  	public boolean isMutable() {
74  		return true;
75  	}
76  
77  	@Override
78  	public boolean equals(Object x, Object y) {
79  		return (x == y) ||
80  				(x instanceof byte[] && y instanceof byte[] && Arrays.equals((byte[]) x, (byte[]) y));
81  	}
82  
83  	@Override
84  	public Object deepCopy(Object value) {
85  		if (value == null) {
86  			return null;
87  		}
88  		byte[] original = (byte[]) value;
89  		byte[] copy = new byte[original.length];
90  		System.arraycopy(original, 0, copy, 0, original.length);
91  		return copy;
92  	}
93  
94  	@Override
95  	protected Object nullSafeGetInternal(
96  			ResultSet rs, String[] names, Object owner, LobHandler lobHandler)
97  			throws SQLException {
98  
99  		return lobHandler.getBlobAsBytes(rs, names[0]);
100 	}
101 
102 	@Override
103 	protected void nullSafeSetInternal(
104 			PreparedStatement ps, int index, Object value, LobCreator lobCreator)
105 			throws SQLException {
106 
107 		lobCreator.setBlobAsBytes(ps, index, (byte[]) value);
108 	}
109 
110 }