1 16 17 package org.springframework.orm.hibernate3.support; 18 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 import java.sql.PreparedStatement ; 22 import java.sql.ResultSet ; 23 import java.sql.SQLException ; 24 25 import javax.transaction.Status ; 26 import javax.transaction.TransactionManager ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.hibernate.HibernateException; 31 import org.hibernate.usertype.UserType; 32 import org.hibernate.util.EqualsHelper; 33 34 import org.springframework.dao.DataAccessResourceFailureException; 35 import org.springframework.jdbc.support.lob.JtaLobCreatorSynchronization; 36 import org.springframework.jdbc.support.lob.LobCreator; 37 import org.springframework.jdbc.support.lob.LobHandler; 38 import org.springframework.jdbc.support.lob.SpringLobCreatorSynchronization; 39 import org.springframework.jdbc.support.lob.LobCreatorUtils; 40 import org.springframework.orm.hibernate3.LocalSessionFactoryBean; 41 import org.springframework.transaction.support.TransactionSynchronizationManager; 42 43 62 public abstract class AbstractLobType implements UserType { 63 64 protected final Log logger = LogFactory.getLog(getClass()); 65 66 private final LobHandler lobHandler; 67 68 private final TransactionManager jtaTransactionManager; 69 70 71 77 protected AbstractLobType() { 78 this(LocalSessionFactoryBean.getConfigTimeLobHandler(), 79 LocalSessionFactoryBean.getConfigTimeTransactionManager()); 80 } 81 82 86 protected AbstractLobType(LobHandler lobHandler, TransactionManager jtaTransactionManager) { 87 this.lobHandler = lobHandler; 88 this.jtaTransactionManager = jtaTransactionManager; 89 } 90 91 92 95 public boolean isMutable() { 96 return false; 97 } 98 99 103 public boolean equals(Object x, Object y) throws HibernateException { 104 return EqualsHelper.equals(x, y); 105 } 106 107 110 public int hashCode(Object x) throws HibernateException { 111 return x.hashCode(); 112 } 113 114 117 public Object deepCopy(Object value) throws HibernateException { 118 return value; 119 } 120 121 124 public Serializable disassemble(Object value) throws HibernateException { 125 return (Serializable ) value; 126 } 127 128 131 public Object assemble(Serializable cached, Object owner) throws HibernateException { 132 return cached; 133 } 134 135 138 public Object replace(Object original, Object target, Object owner) throws HibernateException { 139 return original; 140 } 141 142 143 148 public final Object nullSafeGet(ResultSet rs, String [] names, Object owner) 149 throws HibernateException, SQLException { 150 151 if (this.lobHandler == null) { 152 throw new IllegalStateException ("No LobHandler found for configuration - " + 153 "lobHandler property must be set on LocalSessionFactoryBean"); 154 } 155 156 try { 157 return nullSafeGetInternal(rs, names, owner, this.lobHandler); 158 } 159 catch (IOException ex) { 160 throw new HibernateException("I/O errors during LOB access", ex); 161 } 162 } 163 164 170 public final void nullSafeSet(PreparedStatement st, Object value, int index) 171 throws HibernateException, SQLException { 172 173 if (this.lobHandler == null) { 174 throw new IllegalStateException ("No LobHandler found for configuration - " + 175 "lobHandler property must be set on LocalSessionFactoryBean"); 176 } 177 178 LobCreator lobCreator = this.lobHandler.getLobCreator(); 179 try { 180 nullSafeSetInternal(st, index, value, lobCreator); 181 } 182 catch (IOException ex) { 183 throw new HibernateException("I/O errors during LOB access", ex); 184 } 185 LobCreatorUtils.registerTransactionSynchronization(lobCreator, this.jtaTransactionManager); 186 } 187 188 199 protected abstract Object nullSafeGetInternal( 200 ResultSet rs, String [] names, Object owner, LobHandler lobHandler) 201 throws SQLException , IOException , HibernateException; 202 203 213 protected abstract void nullSafeSetInternal( 214 PreparedStatement ps, int index, Object value, LobCreator lobCreator) 215 throws SQLException , IOException , HibernateException; 216 217 } 218 | Popular Tags |