org.hibernate.type.descriptor.java.ClobTypeDescriptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
JPMS Module-Info's for a few of the Jakarta Libraries just until they add them in themselves
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.type.descriptor.java;
import java.io.Reader;
import java.io.Serializable;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.Comparator;
import org.hibernate.HibernateException;
import org.hibernate.engine.jdbc.CharacterStream;
import org.hibernate.engine.jdbc.ClobImplementer;
import org.hibernate.engine.jdbc.ClobProxy;
import org.hibernate.engine.jdbc.WrappedClob;
import org.hibernate.engine.jdbc.internal.CharacterStreamImpl;
import org.hibernate.type.descriptor.WrapperOptions;
/**
* Descriptor for {@link Clob} handling.
*
* Note, {@link Clob clobs} really are mutable (their internal state can in fact be mutated). We simply
* treat them as immutable because we cannot properly check them for changes nor deep copy them.
*
* @author Steve Ebersole
*/
public class ClobTypeDescriptor extends AbstractTypeDescriptor {
public static final ClobTypeDescriptor INSTANCE = new ClobTypeDescriptor();
public static class ClobMutabilityPlan implements MutabilityPlan {
public static final ClobMutabilityPlan INSTANCE = new ClobMutabilityPlan();
public boolean isMutable() {
return false;
}
public Clob deepCopy(Clob value) {
return value;
}
public Serializable disassemble(Clob value) {
throw new UnsupportedOperationException( "Clobs are not cacheable" );
}
public Clob assemble(Serializable cached) {
throw new UnsupportedOperationException( "Clobs are not cacheable" );
}
}
public ClobTypeDescriptor() {
super( Clob.class, ClobMutabilityPlan.INSTANCE );
}
@Override
public String extractLoggableRepresentation(Clob value) {
return value == null ? "null" : "{clob}";
}
public String toString(Clob value) {
return DataHelper.extractString( value );
}
public Clob fromString(String string) {
return ClobProxy.generateProxy( string );
}
@Override
@SuppressWarnings({ "unchecked" })
public Comparator getComparator() {
return IncomparableComparator.INSTANCE;
}
@Override
public int extractHashCode(Clob value) {
return System.identityHashCode( value );
}
@Override
public boolean areEqual(Clob one, Clob another) {
return one == another;
}
@SuppressWarnings({ "unchecked" })
public X unwrap(final Clob value, Class type, WrapperOptions options) {
if ( value == null ) {
return null;
}
try {
if ( CharacterStream.class.isAssignableFrom( type ) ) {
if (value instanceof ClobImplementer) {
// if the incoming Clob is a wrapper, just pass along its CharacterStream
return (X) ( (ClobImplementer) value ).getUnderlyingStream();
}
else {
// otherwise we need to build a CharacterStream...
return (X) new CharacterStreamImpl( DataHelper.extractString( value.getCharacterStream() ) );
}
}
else if (Clob.class.isAssignableFrom( type )) {
final Clob clob = value instanceof WrappedClob
? ( (WrappedClob) value ).getWrappedClob()
: value;
return (X) clob;
}
else if ( String.class.isAssignableFrom( type ) ) {
if (value instanceof ClobImplementer) {
// if the incoming Clob is a wrapper, just get the underlying String.
return (X) ( (ClobImplementer) value ).getUnderlyingStream().asString();
}
else {
// otherwise we need to extract the String.
return (X) DataHelper.extractString( value.getCharacterStream() );
}
}
}
catch ( SQLException e ) {
throw new HibernateException( "Unable to access clob stream", e );
}
throw unknownUnwrap( type );
}
public Clob wrap(X value, WrapperOptions options) {
if ( value == null ) {
return null;
}
// Support multiple return types from
// org.hibernate.type.descriptor.sql.ClobTypeDescriptor
if ( Clob.class.isAssignableFrom( value.getClass() ) ) {
return options.getLobCreator().wrap( (Clob) value );
}
else if ( Reader.class.isAssignableFrom( value.getClass() ) ) {
Reader reader = (Reader) value;
return options.getLobCreator().createClob( DataHelper.extractString( reader ) );
}
else if ( String.class.isAssignableFrom( value.getClass() ) ) {
return options.getLobCreator().createClob( (String) value );
}
throw unknownWrap( value.getClass() );
}
}