Skip to content

Commit af82013

Browse files
committed
HHH-19587 - Easier access to LobHelper
The getLobHelper method was moved to the org.hibernate.Hibernate class and deprecated in Session Signed-off-by: Jan Schatteman <[email protected]>
1 parent ce25d71 commit af82013

28 files changed

+149
-112
lines changed

hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/AltibaseFunctionsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import static org.hamcrest.CoreMatchers.is;
2323
import static org.hamcrest.MatcherAssert.assertThat;
24+
import static org.hibernate.Hibernate.getLobHelper;
2425
import static org.junit.jupiter.api.Assertions.assertNotNull;
2526

2627
@DomainModel(annotatedClasses = Person.class)
@@ -49,8 +50,8 @@ public void createTestData(SessionFactoryScope scope) {
4950
{
5051
arry[i] = (byte)i;
5152
}
52-
person.setBinaryData( session.getLobHelper().createBlob(arry) );
53-
person.setComments( session.getLobHelper().createClob("blahblah") );
53+
person.setBinaryData( getLobHelper().createBlob(arry) );
54+
person.setComments( getLobHelper().createClob("blahblah") );
5455
session.persist( person );
5556
}
5657
);

hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/functional/cache/SQLFunctionsInterSystemsTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.hibernate.orm.test.legacy.Single;
3333
import org.junit.Test;
3434

35+
import static org.hibernate.Hibernate.getLobHelper;
3536
import static org.junit.Assert.assertEquals;
3637
import static org.junit.Assert.assertFalse;
3738
import static org.junit.Assert.assertNotNull;
@@ -470,8 +471,8 @@ public void testBlobClob() throws Exception {
470471
Session s = openSession();
471472
s.beginTransaction();
472473
Blobber b = new Blobber();
473-
b.setBlob( s.getLobHelper().createBlob( "foo/bar/baz".getBytes() ) );
474-
b.setClob( s.getLobHelper().createClob("foo/bar/baz") );
474+
b.setBlob( getLobHelper().createBlob( "foo/bar/baz".getBytes() ) );
475+
b.setClob( getLobHelper().createClob("foo/bar/baz") );
475476
s.persist(b);
476477
//s.refresh(b);
477478
//assertTrue( b.getClob() instanceof ClobImpl );
@@ -502,7 +503,7 @@ public void testBlobClob() throws Exception {
502503
s = openSession();
503504
s.beginTransaction();
504505
b = s.getReference( Blobber.class, b.getId() );
505-
b.setClob( s.getLobHelper().createClob("xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb") );
506+
b.setClob( getLobHelper().createClob("xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb") );
506507
s.flush();
507508
s.getTransaction().commit();
508509
s.close();

hibernate-core/src/main/java/org/hibernate/Hibernate.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
*/
55
package org.hibernate;
66

7+
import java.io.InputStream;
8+
import java.io.Reader;
9+
import java.sql.Blob;
10+
import java.sql.Clob;
11+
import java.sql.NClob;
712
import java.util.ArrayList;
813
import java.util.Collection;
914
import java.util.HashMap;
@@ -27,6 +32,8 @@
2732
import org.hibernate.collection.spi.PersistentSortedMap;
2833
import org.hibernate.collection.spi.PersistentSortedSet;
2934
import org.hibernate.collection.spi.PersistentCollection;
35+
import org.hibernate.engine.jdbc.LobCreator;
36+
import org.hibernate.engine.jdbc.env.internal.NonContextualLobCreator;
3037
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
3138
import org.hibernate.engine.spi.SessionFactoryImplementor;
3239
import org.hibernate.persister.entity.EntityPersister;
@@ -111,6 +118,8 @@ private Hibernate() {
111118
throw new UnsupportedOperationException();
112119
}
113120

121+
private static final LobHelperImpl lobHelper = new LobHelperImpl();
122+
114123
/**
115124
* Force initialization of a proxy or persistent collection. In the case of a
116125
* many-valued association, only the collection itself is initialized. It is not
@@ -587,7 +596,56 @@ else if (collectionClass == Collection.class) {
587596
}
588597
}
589598

599+
/**
600+
* Obtain a {@linkplain LobHelper} for instances of {@link java.sql.Blob}
601+
* and {@link java.sql.Clob}.
602+
*
603+
* @return an instance of {@link LobHelper}
604+
*/
605+
public static LobHelper getLobHelper() {
606+
return lobHelper;
607+
}
608+
590609
private static PersistentAttributeInterceptor getAttributeInterceptor(Object entity) {
591610
return asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor();
592611
}
612+
613+
private static class LobHelperImpl implements LobHelper {
614+
@Override
615+
public Blob createBlob(byte[] bytes) {
616+
return lobCreator().createBlob( bytes );
617+
}
618+
619+
private LobCreator lobCreator() {
620+
// Always use NonContextualLobCreator. If ContextualLobCreator is
621+
// used both here and in WrapperOptions,
622+
return NonContextualLobCreator.INSTANCE;
623+
}
624+
625+
@Override
626+
public Blob createBlob(InputStream stream, long length) {
627+
return lobCreator().createBlob( stream, length );
628+
}
629+
630+
@Override
631+
public Clob createClob(String string) {
632+
return lobCreator().createClob( string );
633+
}
634+
635+
@Override
636+
public Clob createClob(Reader reader, long length) {
637+
return lobCreator().createClob( reader, length );
638+
}
639+
640+
@Override
641+
public NClob createNClob(String string) {
642+
return lobCreator().createNClob( string );
643+
}
644+
645+
@Override
646+
public NClob createNClob(Reader reader, long length) {
647+
return lobCreator().createNClob( reader, length );
648+
}
649+
}
650+
593651
}

hibernate-core/src/main/java/org/hibernate/LobHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @author Steve Ebersole
1717
*
18-
* @see Session#getLobHelper()
18+
* @see Hibernate#getLobHelper()
1919
*/
2020
public interface LobHelper {
2121

hibernate-core/src/main/java/org/hibernate/Session.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,11 +1321,15 @@ public interface Session extends SharedSessionContract, EntityManager {
13211321
void disableFetchProfile(String name) throws UnknownProfileException;
13221322

13231323
/**
1324-
* Obtain a {@linkplain LobHelper factory} for instances of {@link java.sql.Blob}
1324+
* Obtain a {@linkplain LobHelper} for instances of {@link java.sql.Blob}
13251325
* and {@link java.sql.Clob}.
13261326
*
13271327
* @return an instance of {@link LobHelper}
1328+
*
1329+
* @deprecated This method will be removed.
1330+
* use {@link Hibernate#getLobHelper()} instead
13281331
*/
1332+
@Deprecated(since="7.0", forRemoval = true)
13291333
LobHelper getLobHelper();
13301334

13311335
/**

hibernate-core/src/main/java/org/hibernate/engine/jdbc/LobCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Contract for creating various LOB references.
1515
*
1616
* @apiNote This class is not intended to be called directly by the application program.
17-
* Instead, use {@link org.hibernate.Session#getLobHelper()}.
17+
* Instead, use {@link org.hibernate.Hibernate#getLobHelper()}.
1818
*
1919
* @author Steve Ebersole
2020
* @author Gail Badner

hibernate-core/src/main/java/org/hibernate/engine/jdbc/proxy/BlobProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Manages aspects of representing {@link Blob} objects.
2323
*
2424
* @apiNote This class is not intended to be called directly by the application program.
25-
* Instead, use {@link org.hibernate.Session#getLobHelper()}.
25+
* Instead, use {@link org.hibernate.Hibernate#getLobHelper()}.
2626
*
2727
* @see ClobProxy
2828
* @see LobCreator

hibernate-core/src/main/java/org/hibernate/engine/jdbc/proxy/ClobProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* handling proxy invocations. We use proxies here solely to avoid JDBC version incompatibilities.
2727
*
2828
* @apiNote This class is not intended to be called directly by the application program.
29-
* Instead, use {@link org.hibernate.Session#getLobHelper()}.
29+
* Instead, use {@link org.hibernate.Hibernate#getLobHelper()}.
3030
*
3131
* @see NClobProxy
3232
* @see BlobProxy

hibernate-core/src/main/java/org/hibernate/engine/jdbc/proxy/NClobProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* handling proxy invocations. We use proxies here solely to avoid JDBC version incompatibilities.
1717
*
1818
* @apiNote This class is not intended to be called directly by the application program.
19-
* Instead, use {@link org.hibernate.Session#getLobHelper()}.
19+
* Instead, use {@link org.hibernate.Hibernate#getLobHelper()}.
2020
*
2121
* @see ClobProxy
2222
* @see BlobProxy

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
2727
import org.hibernate.collection.spi.PersistentCollection;
2828
import org.hibernate.engine.internal.PersistenceContexts;
29-
import org.hibernate.engine.jdbc.LobCreator;
30-
import org.hibernate.engine.jdbc.env.internal.NonContextualLobCreator;
3129
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
3230
import org.hibernate.engine.spi.ActionQueue;
3331
import org.hibernate.engine.spi.ActionQueue.TransactionCompletionProcesses;
@@ -86,16 +84,11 @@
8684
import org.hibernate.type.descriptor.WrapperOptions;
8785

8886
import java.io.IOException;
89-
import java.io.InputStream;
9087
import java.io.ObjectInputStream;
9188
import java.io.ObjectOutputStream;
92-
import java.io.Reader;
9389
import java.io.Serial;
9490
import java.io.Serializable;
95-
import java.sql.Blob;
96-
import java.sql.Clob;
9791
import java.sql.Connection;
98-
import java.sql.NClob;
9992
import java.sql.SQLException;
10093
import java.util.Collection;
10194
import java.util.HashMap;
@@ -2026,14 +2019,9 @@ public int getFetchBatchSize() {
20262019

20272020
@Override
20282021
public LobHelper getLobHelper() {
2029-
if ( lobHelper == null ) {
2030-
lobHelper = new LobHelperImpl();
2031-
}
2032-
return lobHelper;
2022+
return Hibernate.getLobHelper();
20332023
}
20342024

2035-
private transient LobHelperImpl lobHelper;
2036-
20372025
@Override
20382026
public void beforeTransactionCompletion() {
20392027
log.trace( "SessionImpl#beforeTransactionCompletion()" );
@@ -2067,45 +2055,6 @@ public void afterTransactionCompletion(boolean successful, boolean delayed) {
20672055
super.afterTransactionCompletion( successful, delayed );
20682056
}
20692057

2070-
private static class LobHelperImpl implements LobHelper {
2071-
2072-
@Override
2073-
public Blob createBlob(byte[] bytes) {
2074-
return lobCreator().createBlob( bytes );
2075-
}
2076-
2077-
private LobCreator lobCreator() {
2078-
// Always use NonContextualLobCreator. If ContextualLobCreator is
2079-
// used both here and in WrapperOptions,
2080-
return NonContextualLobCreator.INSTANCE;
2081-
}
2082-
2083-
@Override
2084-
public Blob createBlob(InputStream stream, long length) {
2085-
return lobCreator().createBlob( stream, length );
2086-
}
2087-
2088-
@Override
2089-
public Clob createClob(String string) {
2090-
return lobCreator().createClob( string );
2091-
}
2092-
2093-
@Override
2094-
public Clob createClob(Reader reader, long length) {
2095-
return lobCreator().createClob( reader, length );
2096-
}
2097-
2098-
@Override
2099-
public NClob createNClob(String string) {
2100-
return lobCreator().createNClob( string );
2101-
}
2102-
2103-
@Override
2104-
public NClob createNClob(Reader reader, long length) {
2105-
return lobCreator().createNClob( reader, length );
2106-
}
2107-
}
2108-
21092058
private static class SharedSessionBuilderImpl
21102059
extends SessionFactoryImpl.SessionBuilderImpl
21112060
implements SharedSessionBuilder, SharedSessionCreationOptions {

0 commit comments

Comments
 (0)