Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hibernate.Hibernate.getLobHelper;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@DomainModel(annotatedClasses = Person.class)
Expand Down Expand Up @@ -49,8 +50,8 @@ public void createTestData(SessionFactoryScope scope) {
{
arry[i] = (byte)i;
}
person.setBinaryData( session.getLobHelper().createBlob(arry) );
person.setComments( session.getLobHelper().createClob("blahblah") );
person.setBinaryData( getLobHelper().createBlob(arry) );
person.setComments( getLobHelper().createClob("blahblah") );
session.persist( person );
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.hibernate.orm.test.legacy.Single;
import org.junit.Test;

import static org.hibernate.Hibernate.getLobHelper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -470,8 +471,8 @@ public void testBlobClob() throws Exception {
Session s = openSession();
s.beginTransaction();
Blobber b = new Blobber();
b.setBlob( s.getLobHelper().createBlob( "foo/bar/baz".getBytes() ) );
b.setClob( s.getLobHelper().createClob("foo/bar/baz") );
b.setBlob( getLobHelper().createBlob( "foo/bar/baz".getBytes() ) );
b.setClob( getLobHelper().createClob("foo/bar/baz") );
s.persist(b);
//s.refresh(b);
//assertTrue( b.getClob() instanceof ClobImpl );
Expand Down Expand Up @@ -502,7 +503,7 @@ public void testBlobClob() throws Exception {
s = openSession();
s.beginTransaction();
b = s.getReference( Blobber.class, b.getId() );
b.setClob( s.getLobHelper().createClob("xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb") );
b.setClob( getLobHelper().createClob("xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb") );
s.flush();
s.getTransaction().commit();
s.close();
Expand Down
58 changes: 58 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/Hibernate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
*/
package org.hibernate;

import java.io.InputStream;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.NClob;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -27,6 +32,8 @@
import org.hibernate.collection.spi.PersistentSortedMap;
import org.hibernate.collection.spi.PersistentSortedSet;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.env.internal.NonContextualLobCreator;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.persister.entity.EntityPersister;
Expand Down Expand Up @@ -111,6 +118,8 @@ private Hibernate() {
throw new UnsupportedOperationException();
}

private static final LobHelperImpl lobHelper = new LobHelperImpl();

/**
* Force initialization of a proxy or persistent collection. In the case of a
* many-valued association, only the collection itself is initialized. It is not
Expand Down Expand Up @@ -587,7 +596,56 @@ else if (collectionClass == Collection.class) {
}
}

/**
* Obtain a {@linkplain LobHelper} for instances of {@link java.sql.Blob}
* and {@link java.sql.Clob}.
*
* @return an instance of {@link LobHelper}
*/
public static LobHelper getLobHelper() {
return lobHelper;
}

private static PersistentAttributeInterceptor getAttributeInterceptor(Object entity) {
return asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor();
}

private static class LobHelperImpl implements LobHelper {
@Override
public Blob createBlob(byte[] bytes) {
return lobCreator().createBlob( bytes );
}

private LobCreator lobCreator() {
// Always use NonContextualLobCreator. If ContextualLobCreator is
// used both here and in WrapperOptions,
return NonContextualLobCreator.INSTANCE;
}

@Override
public Blob createBlob(InputStream stream, long length) {
return lobCreator().createBlob( stream, length );
}

@Override
public Clob createClob(String string) {
return lobCreator().createClob( string );
}

@Override
public Clob createClob(Reader reader, long length) {
return lobCreator().createClob( reader, length );
}

@Override
public NClob createNClob(String string) {
return lobCreator().createNClob( string );
}

@Override
public NClob createNClob(Reader reader, long length) {
return lobCreator().createNClob( reader, length );
}
}

}
2 changes: 1 addition & 1 deletion hibernate-core/src/main/java/org/hibernate/LobHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @author Steve Ebersole
*
* @see Session#getLobHelper()
* @see Hibernate#getLobHelper()
*/
public interface LobHelper {

Expand Down
6 changes: 5 additions & 1 deletion hibernate-core/src/main/java/org/hibernate/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -1321,11 +1321,15 @@ public interface Session extends SharedSessionContract, EntityManager {
void disableFetchProfile(String name) throws UnknownProfileException;

/**
* Obtain a {@linkplain LobHelper factory} for instances of {@link java.sql.Blob}
* Obtain a {@linkplain LobHelper} for instances of {@link java.sql.Blob}
* and {@link java.sql.Clob}.
*
* @return an instance of {@link LobHelper}
*
* @deprecated This method will be removed.
* use {@link Hibernate#getLobHelper()} instead
*/
@Deprecated(since="7.0", forRemoval = true)
LobHelper getLobHelper();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Contract for creating various LOB references.
*
* @apiNote This class is not intended to be called directly by the application program.
* Instead, use {@link org.hibernate.Session#getLobHelper()}.
* Instead, use {@link org.hibernate.Hibernate#getLobHelper()}.
*
* @author Steve Ebersole
* @author Gail Badner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Manages aspects of representing {@link Blob} objects.
*
* @apiNote This class is not intended to be called directly by the application program.
* Instead, use {@link org.hibernate.Session#getLobHelper()}.
* Instead, use {@link org.hibernate.Hibernate#getLobHelper()}.
*
* @see ClobProxy
* @see LobCreator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* handling proxy invocations. We use proxies here solely to avoid JDBC version incompatibilities.
*
* @apiNote This class is not intended to be called directly by the application program.
* Instead, use {@link org.hibernate.Session#getLobHelper()}.
* Instead, use {@link org.hibernate.Hibernate#getLobHelper()}.
*
* @see NClobProxy
* @see BlobProxy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* handling proxy invocations. We use proxies here solely to avoid JDBC version incompatibilities.
*
* @apiNote This class is not intended to be called directly by the application program.
* Instead, use {@link org.hibernate.Session#getLobHelper()}.
* Instead, use {@link org.hibernate.Hibernate#getLobHelper()}.
*
* @see ClobProxy
* @see BlobProxy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.internal.PersistenceContexts;
import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.env.internal.NonContextualLobCreator;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.spi.ActionQueue;
import org.hibernate.engine.spi.ActionQueue.TransactionCompletionProcesses;
Expand Down Expand Up @@ -86,16 +84,11 @@
import org.hibernate.type.descriptor.WrapperOptions;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.Serial;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.NClob;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -2026,14 +2019,9 @@ public int getFetchBatchSize() {

@Override
public LobHelper getLobHelper() {
if ( lobHelper == null ) {
lobHelper = new LobHelperImpl();
}
return lobHelper;
return Hibernate.getLobHelper();
}

private transient LobHelperImpl lobHelper;

@Override
public void beforeTransactionCompletion() {
log.trace( "SessionImpl#beforeTransactionCompletion()" );
Expand Down Expand Up @@ -2067,45 +2055,6 @@ public void afterTransactionCompletion(boolean successful, boolean delayed) {
super.afterTransactionCompletion( successful, delayed );
}

private static class LobHelperImpl implements LobHelper {

@Override
public Blob createBlob(byte[] bytes) {
return lobCreator().createBlob( bytes );
}

private LobCreator lobCreator() {
// Always use NonContextualLobCreator. If ContextualLobCreator is
// used both here and in WrapperOptions,
return NonContextualLobCreator.INSTANCE;
}

@Override
public Blob createBlob(InputStream stream, long length) {
return lobCreator().createBlob( stream, length );
}

@Override
public Clob createClob(String string) {
return lobCreator().createClob( string );
}

@Override
public Clob createClob(Reader reader, long length) {
return lobCreator().createClob( reader, length );
}

@Override
public NClob createNClob(String string) {
return lobCreator().createNClob( string );
}

@Override
public NClob createNClob(Reader reader, long length) {
return lobCreator().createNClob( reader, length );
}
}

private static class SharedSessionBuilderImpl
extends SessionFactoryImpl.SessionBuilderImpl
implements SharedSessionBuilder, SharedSessionCreationOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

import static org.hibernate.Hibernate.getLobHelper;

/**
* To reproduce this issue, Oracle MUST use a multi-byte character set (UTF-8)!
*
Expand All @@ -29,7 +31,7 @@ public void hibernateTest() {
session.beginTransaction();
LobTestEntity entity = new LobTestEntity();
entity.setId(1L);
entity.setLobValue(session.getLobHelper().createBlob(new byte[9999]));
entity.setLobValue(getLobHelper().createBlob(new byte[9999]));
entity.setQwerty(randomString(4000));
session.persist(entity);
session.getTransaction().commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.type.descriptor.java.DataHelper;

import static org.hibernate.Hibernate.getLobHelper;

/**
* @author Lukasz Antoniak
*/
Expand All @@ -38,8 +40,8 @@ public void testStreamResetBeforeParameterBinding() throws SQLException {

session.getTransaction().begin();
LobHolder entity = new LobHolder(
session.getLobHelper().createBlob( "blob".getBytes() ),
session.getLobHelper().createClob( "clob" ), 0
getLobHelper().createBlob( "blob".getBytes() ),
getLobHelper().createClob( "clob" ), 0
);
session.persist( entity );
session.getTransaction().commit();
Expand All @@ -56,8 +58,8 @@ public void testStreamResetBeforeParameterBinding() throws SQLException {

session.getTransaction().begin();
entity = (LobHolder) session.get( LobHolder.class, entity.getId() );
entity.setBlobLocator( session.getLobHelper().createBlob( "updated blob".getBytes() ) );
entity.setClobLocator( session.getLobHelper().createClob( "updated clob" ) );
entity.setBlobLocator( getLobHelper().createBlob( "updated blob".getBytes() ) );
entity.setClobLocator( getLobHelper().createClob( "updated clob" ) );
entity = (LobHolder) session.merge( entity );
session.getTransaction().commit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.cfg.Environment;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;

import static org.hibernate.Hibernate.getLobHelper;
import static org.junit.Assert.assertFalse;

/**
Expand Down Expand Up @@ -44,9 +45,9 @@ public void configure(Configuration cfg) {
public void testLobCreation() throws SQLException {
Session session = sessionFactory().getCurrentSession();
session.beginTransaction();
Blob blob = session.getLobHelper().createBlob( new byte[] {} );
Blob blob = getLobHelper().createBlob( new byte[] {} );
blob.free();
Clob clob = session.getLobHelper().createClob( "Steve" );
Clob clob = getLobHelper().createClob( "Steve" );
clob.free();
session.getTransaction().commit();
assertFalse( session.isOpen() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.Test;

import static org.hibernate.Hibernate.getLobHelper;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -44,10 +45,10 @@ public void testInterfaceProxies(SessionFactoryScope scope) {
session.beginTransaction();
try {
doc.setName( "Hibernate in Action" );
doc.setContent( session.getLobHelper().createBlob( "blah blah blah".getBytes() ) );
doc.setContent( getLobHelper().createBlob( "blah blah blah".getBytes() ) );
session.persist( doc );
doc2.setName( "Secret" );
doc2.setContent( session.getLobHelper().createBlob( "wxyz wxyz".getBytes() ) );
doc2.setContent( getLobHelper().createBlob( "wxyz wxyz".getBytes() ) );
// SybaseASE15Dialect only allows 7-bits in a byte to be inserted into a tinyint
// column (0 <= val < 128)
doc2.setPermissionBits( (byte) 127 );
Expand Down
Loading