HHH-5245 - Introduce LobHelper

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19570 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-05-20 20:19:34 +00:00
parent 5c7f4ee712
commit d06e11092d
5 changed files with 201 additions and 13 deletions

View File

@ -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 );
}

View File

@ -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);
}

View File

@ -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).

View File

@ -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.

View File

@ -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 );