[hibernate-commits] Hibernate SVN: r19570 - in core/trunk/core/src/main/java/org/hibernate: engine/jdbc and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu May 20 16:19:34 EDT 2010


Author: steve.ebersole at jboss.com
Date: 2010-05-20 16:19:34 -0400 (Thu, 20 May 2010)
New Revision: 19570

Added:
   core/trunk/core/src/main/java/org/hibernate/LobHelper.java
Modified:
   core/trunk/core/src/main/java/org/hibernate/Hibernate.java
   core/trunk/core/src/main/java/org/hibernate/Session.java
   core/trunk/core/src/main/java/org/hibernate/engine/jdbc/LobCreationContext.java
   core/trunk/core/src/main/java/org/hibernate/impl/SessionImpl.java
Log:
HHH-5245 - Introduce LobHelper


Modified: core/trunk/core/src/main/java/org/hibernate/Hibernate.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/Hibernate.java	2010-05-20 17:57:24 UTC (rev 19569)
+++ core/trunk/core/src/main/java/org/hibernate/Hibernate.java	2010-05-20 20:19:34 UTC (rev 19570)
@@ -438,7 +438,7 @@
 	 *
 	 * @param bytes a byte array
 	 * @return the Blob
-	 * @deprecated Use {@link #createBlob(byte[], Session)} instead
+	 * @deprecated Use {@link LobHelper#createBlob(byte[])} instead.
 	 */
 	public static Blob createBlob(byte[] bytes) {
 		return NonContextualLobCreator.INSTANCE.wrap(
@@ -452,9 +452,9 @@
 	 * @param bytes a byte array
 	 * @param session The session in which the {@link Blob} will be used.
 	 * @return the Blob
+	 * @deprecated Use {@link LobHelper#createBlob(byte[])} instead.
 	 */
 	public static Blob createBlob(byte[] bytes, Session session) {
-		// todo : wrap?
 		return getLobCreator( session ).createBlob( bytes );
 	}
 
@@ -475,8 +475,9 @@
 	 * @param stream a binary stream
 	 * @param length the number of bytes in the stream
 	 * @return the Blob
-	 * @deprecated Use {@link #createBlob(InputStream, long, Session)} instead
+	 * @deprecated Use {@link LobHelper#createBlob(InputStream, long)} instead.
 	 */
+	@Deprecated
 	public static Blob createBlob(InputStream stream, int length) {
 		return NonContextualLobCreator.INSTANCE.wrap(
 				NonContextualLobCreator.INSTANCE.createBlob( stream, length )
@@ -489,8 +490,9 @@
 	 * @param stream a binary stream
 	 * @param length the number of bytes in the stream
 	 * @return the Blob
-	 * @deprecated Use {@link #createBlob(InputStream, long, Session)} instead
+	 * @deprecated Use {@link LobHelper#createBlob(InputStream, long)} instead.
 	 */
+	@Deprecated
 	public static Blob createBlob(InputStream stream, long length) {
 		return NonContextualLobCreator.INSTANCE.wrap(
 				NonContextualLobCreator.INSTANCE.createBlob( stream, length )
@@ -504,9 +506,10 @@
 	 * @param length the number of bytes in the stream
 	 * @param session The session in which the {@link Blob} will be used.
 	 * @return the Blob
+	 * @deprecated Use {@link LobHelper#createBlob(InputStream, long)} instead.
 	 */
+	@Deprecated
 	public static Blob createBlob(InputStream stream, long length, Session session) {
-		// todo : wrap?
 		return getLobCreator( session ).createBlob( stream, length );
 	}
 
@@ -520,8 +523,9 @@
 	 * @param stream a binary stream
 	 * @return the Blob
 	 * @throws IOException Indicates an I/O problem accessing the stream
-	 * @deprecated Use {@link #createBlob(InputStream, long, Session)} instead
+	 * @deprecated With no direct replacement.  Use {@link #createBlob(InputStream,long)} instead, passing in the length
 	 */
+	@Deprecated
 	public static Blob createBlob(InputStream stream) throws IOException {
 		ByteArrayOutputStream buffer = new ByteArrayOutputStream( stream.available() );
 		StreamUtils.copy( stream, buffer );
@@ -533,8 +537,9 @@
 	 *
 	 * @param string The string data
 	 * @return The created {@link Clob}
-	 * @deprecated Use {@link #createClob(String, Session)} instead
+	 * @deprecated Use {@link LobHelper#createClob(String)} instead
 	 */
+	@Deprecated
 	public static Clob createClob(String string) {
 		return NonContextualLobCreator.INSTANCE.wrap(
 				NonContextualLobCreator.INSTANCE.createClob( string )
@@ -547,9 +552,10 @@
 	 * @param string The string data
 	 * @param session The session in which the {@link Clob} will be used.
 	 * @return The created {@link Clob}
+	 * @deprecated Use {@link LobHelper#createClob(String)} instead
 	 */
+	@Deprecated
 	public static Clob createClob(String string, Session session) {
-		// todo : wrap?
 		return getLobCreator( session ).createClob( string );
 	}
 
@@ -559,8 +565,9 @@
 	 * @param reader a character stream
 	 * @param length the number of characters in the stream
 	 * @return The created {@link Clob}
-	 * @deprecated Use {@link #createClob(Reader,long,Session)} instead
+	 * @deprecated Use {@link LobHelper#createClob(Reader, long)} instead
 	 */
+	@Deprecated
 	public static Clob createClob(Reader reader, int length) {
 		return NonContextualLobCreator.INSTANCE.wrap(
 				NonContextualLobCreator.INSTANCE.createClob( reader, length )
@@ -573,8 +580,9 @@
 	 * @param reader a character stream
 	 * @param length the number of characters in the stream
 	 * @return The created {@link Clob}
-	 * @deprecated Use {@link #createClob(Reader,long,Session)} instead
+	 * @deprecated Use {@link LobHelper#createClob(Reader, long)} instead
 	 */
+	@Deprecated
 	public static Clob createClob(Reader reader, long length) {
 		return NonContextualLobCreator.INSTANCE.wrap(
 				NonContextualLobCreator.INSTANCE.createClob( reader, length ) 
@@ -588,7 +596,9 @@
 	 * @param length the number of characters in the stream
 	 * @param session The session in which the {@link Clob} will be used.
 	 * @return The created {@link Clob}
+	 * @deprecated Use {@link LobHelper#createClob(Reader, long)} instead
 	 */
+	@Deprecated
 	public static Clob createClob(Reader reader, long length, Session session) {
 		return getLobCreator( session ).createClob( reader, length );
 	}

Added: core/trunk/core/src/main/java/org/hibernate/LobHelper.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/LobHelper.java	                        (rev 0)
+++ core/trunk/core/src/main/java/org/hibernate/LobHelper.java	2010-05-20 20:19:34 UTC (rev 19570)
@@ -0,0 +1,100 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate;
+
+import java.io.InputStream;
+import java.io.Reader;
+import java.sql.Blob;
+import java.sql.Clob;
+
+/**
+ * A {@link Session session's} helper for creating LOB data
+ *
+ * @author Steve Ebersole
+ */
+public interface LobHelper {
+
+	/**
+	 * Create a new {@link Blob} from bytes.
+	 *
+	 * @param bytes a byte array
+	 *
+	 * @return the created Blob
+	 */
+	public Blob createBlob(byte[] bytes);
+
+	/**
+	 * Create a new {@link Blob} from stream data.
+	 *
+	 * @param stream a binary stream
+	 * @param length the number of bytes in the stream
+
+	 * @return the create Blob
+	 */
+	public Blob createBlob(InputStream stream, long length);
+
+	/**
+	 * Create a new {@link java.sql.Clob} from content
+	 *
+	 * @param string The string data
+	 *
+	 * @return The created {@link java.sql.Clob}
+	 */
+	public Clob createClob(String string);
+
+	/**
+	 * Create a new {@link Clob} from character reader.
+	 *
+	 * @param reader a character stream
+	 * @param length the number of characters in the stream
+	 *
+	 * @return The created {@link Clob}
+	 */
+	public Clob createClob(Reader reader, long length);
+
+	/**
+	 * Create a new {@link java.sql.Clob} from content.
+	 * <p/>
+	 * Note, on JDK 1.6+ environments will
+	 * create and return a NClob instead (NClob extends the Clob interface).
+	 *
+	 * @param string The string data
+	 *
+	 * @return The created {@link java.sql.Clob}/NClob
+	 */
+	public Clob createNClob(String string);
+
+	/**
+	 * Create a new {@link Clob} from character reader.
+	 * <p/>
+	 * Note, on JDK 1.6+ environments will
+	 * create and return a NClob instead (NClob extends the Clob interface).
+	 *
+	 * @param reader a character stream
+	 * @param length the number of characters in the stream
+	 *
+	 * @return The created {@link java.sql.Clob}/NClob
+	 */
+	public Clob createNClob(Reader reader, long length);
+}

Modified: core/trunk/core/src/main/java/org/hibernate/Session.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/Session.java	2010-05-20 17:57:24 UTC (rev 19569)
+++ core/trunk/core/src/main/java/org/hibernate/Session.java	2010-05-20 20:19:34 UTC (rev 19570)
@@ -1004,6 +1004,12 @@
 	 */
 	public TypeHelper getTypeHelper();
 
+	/**
+	 * Retrieve this session's helper/delegate for creating LOB instances.
+	 *
+	 * @return This session's LOB helper
+	 */
+	public LobHelper getLobHelper();
 
 	/**
 	 * Contains locking details (LockMode, Timeout and Scope).

Modified: core/trunk/core/src/main/java/org/hibernate/engine/jdbc/LobCreationContext.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/engine/jdbc/LobCreationContext.java	2010-05-20 17:57:24 UTC (rev 19569)
+++ core/trunk/core/src/main/java/org/hibernate/engine/jdbc/LobCreationContext.java	2010-05-20 20:19:34 UTC (rev 19570)
@@ -26,9 +26,6 @@
 import java.sql.Connection;
 import java.sql.SQLException;
 
-import org.hibernate.JDBCException;
-import org.hibernate.exception.SQLExceptionConverter;
-
 /**
  * Provides callback access into the context in which the LOB is to be created.  Mainly this is useful
  * for gaining access to the JDBC {@link Connection} for use in JDBC 4 environments.

Modified: core/trunk/core/src/main/java/org/hibernate/impl/SessionImpl.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/impl/SessionImpl.java	2010-05-20 17:57:24 UTC (rev 19569)
+++ core/trunk/core/src/main/java/org/hibernate/impl/SessionImpl.java	2010-05-20 20:19:34 UTC (rev 19570)
@@ -25,11 +25,15 @@
 package org.hibernate.impl;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.io.Reader;
 import java.io.Serializable;
 import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayInputStream;
+import java.sql.Blob;
+import java.sql.Clob;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Collection;
@@ -52,6 +56,7 @@
 import org.hibernate.FlushMode;
 import org.hibernate.HibernateException;
 import org.hibernate.Interceptor;
+import org.hibernate.LobHelper;
 import org.hibernate.LockMode;
 import org.hibernate.MappingException;
 import org.hibernate.ObjectDeletedException;
@@ -83,6 +88,7 @@
 import org.hibernate.engine.Status;
 import org.hibernate.engine.LoadQueryInfluencers;
 import org.hibernate.engine.jdbc.LobCreationContext;
+import org.hibernate.engine.jdbc.LobCreator;
 import org.hibernate.engine.query.FilterQueryPlan;
 import org.hibernate.engine.query.HQLQueryPlan;
 import org.hibernate.engine.query.NativeSQLQueryPlan;
@@ -2199,6 +2205,9 @@
 		oos.writeObject( childSessionsByEntityMode );
 	}
 
+	/**
+	 * {@inheritDoc}
+	 */
 	public Object execute(Callback callback) {
 		Connection connection = jdbcContext.getConnectionManager().getConnection();
 		try {
@@ -2223,6 +2232,72 @@
 		return getSessionFactory().getTypeHelper();
 	}
 
+	/**
+	 * {@inheritDoc}
+	 */
+	public LobHelper getLobHelper() {
+		if ( lobHelper == null ) {
+			lobHelper = new LobHelperImpl( this );
+		}
+		return lobHelper;
+	}
+
+	private transient LobHelperImpl lobHelper;
+
+	private static class LobHelperImpl implements LobHelper {
+		private final SessionImpl session;
+
+		private LobHelperImpl(SessionImpl session) {
+			this.session = session;
+		}
+
+		/**
+		 * {@inheritDoc}
+		 */
+		public Blob createBlob(byte[] bytes) {
+			return lobCreator().createBlob( bytes );
+		}
+
+		private LobCreator lobCreator() {
+			return session.getFactory().getSettings().getJdbcSupport().getLobCreator( session );
+		}
+
+		/**
+		 * {@inheritDoc}
+		 */
+		public Blob createBlob(InputStream stream, long length) {
+			return lobCreator().createBlob( stream, length );
+		}
+
+		/**
+		 * {@inheritDoc}
+		 */
+		public Clob createClob(String string) {
+			return lobCreator().createClob( string );
+		}
+
+		/**
+		 * {@inheritDoc}
+		 */
+		public Clob createClob(Reader reader, long length) {
+			return lobCreator().createClob( reader, length );
+		}
+
+		/**
+		 * {@inheritDoc}
+		 */
+		public Clob createNClob(String string) {
+			return lobCreator().createNClob( string );
+		}
+
+		/**
+		 * {@inheritDoc}
+		 */
+		public Clob createNClob(Reader reader, long length) {
+			return lobCreator().createNClob( reader, length );
+		}
+	}
+
 	private class CoordinatingEntityNameResolver implements EntityNameResolver {
 		public String resolveEntityName(Object entity) {
 			String entityName = interceptor.getEntityName( entity );



More information about the hibernate-commits mailing list