diff --git a/core/src/main/java/org/hibernate/Hibernate.java b/core/src/main/java/org/hibernate/Hibernate.java index b8d1b925fd..e09673ad93 100644 --- a/core/src/main/java/org/hibernate/Hibernate.java +++ b/core/src/main/java/org/hibernate/Hibernate.java @@ -438,7 +438,7 @@ public final class Hibernate { * * @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 @@ public final class Hibernate { * @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 @@ public final class Hibernate { * @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 @@ public final class Hibernate { * @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 @@ public final class Hibernate { * @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 @@ public final class Hibernate { * @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 @@ public final class Hibernate { * * @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 @@ public final class Hibernate { * @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 @@ public final class Hibernate { * @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 @@ public final class Hibernate { * @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 @@ public final class Hibernate { * @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 ); } diff --git a/core/src/main/java/org/hibernate/LobHelper.java b/core/src/main/java/org/hibernate/LobHelper.java new file mode 100644 index 0000000000..3bcac1342d --- /dev/null +++ b/core/src/main/java/org/hibernate/LobHelper.java @@ -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. + *

+ * 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. + *

+ * 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); +} diff --git a/core/src/main/java/org/hibernate/Session.java b/core/src/main/java/org/hibernate/Session.java index 3c1798fb4e..74a327666e 100644 --- a/core/src/main/java/org/hibernate/Session.java +++ b/core/src/main/java/org/hibernate/Session.java @@ -1004,6 +1004,12 @@ public interface Session extends Serializable { */ 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). diff --git a/core/src/main/java/org/hibernate/engine/jdbc/LobCreationContext.java b/core/src/main/java/org/hibernate/engine/jdbc/LobCreationContext.java index 073c4e2106..bee2017fae 100644 --- a/core/src/main/java/org/hibernate/engine/jdbc/LobCreationContext.java +++ b/core/src/main/java/org/hibernate/engine/jdbc/LobCreationContext.java @@ -26,9 +26,6 @@ package org.hibernate.engine.jdbc; 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. diff --git a/core/src/main/java/org/hibernate/impl/SessionImpl.java b/core/src/main/java/org/hibernate/impl/SessionImpl.java index 7b1b2327a0..3e490d1721 100644 --- a/core/src/main/java/org/hibernate/impl/SessionImpl.java +++ b/core/src/main/java/org/hibernate/impl/SessionImpl.java @@ -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.Filter; 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.StatefulPersistenceContext; 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 @@ public final class SessionImpl extends AbstractSessionImpl oos.writeObject( childSessionsByEntityMode ); } + /** + * {@inheritDoc} + */ public Object execute(Callback callback) { Connection connection = jdbcContext.getConnectionManager().getConnection(); try { @@ -2223,6 +2232,72 @@ public final class SessionImpl extends AbstractSessionImpl 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 );