-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HHH-17294] DeepCopy non-Embedded JSON or XML JdbcTypCode attribute u…
…sing FormatMapper
- Loading branch information
1 parent
2e0f0c5
commit 47e8272
Showing
10 changed files
with
366 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...-core/src/main/java/org/hibernate/type/descriptor/java/spi/FormatMapperBasedJavaType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* 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 http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.type.descriptor.java.spi; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Type; | ||
|
||
import org.hibernate.Incubating; | ||
import org.hibernate.SharedSessionContract; | ||
import org.hibernate.type.descriptor.WrapperOptions; | ||
import org.hibernate.type.descriptor.java.AbstractJavaType; | ||
import org.hibernate.type.descriptor.java.MutabilityPlan; | ||
import org.hibernate.type.descriptor.jdbc.JdbcType; | ||
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; | ||
import org.hibernate.type.format.FormatMapper; | ||
import org.hibernate.type.spi.TypeConfiguration; | ||
|
||
/** | ||
* Java type for {@link FormatMapper} based types i.e. {@link org.hibernate.type.SqlTypes#JSON} | ||
* or {@link org.hibernate.type.SqlTypes#SQLXML} mapped types. | ||
* | ||
* @author Christian Beikov | ||
*/ | ||
@Incubating | ||
public abstract class FormatMapperBasedJavaType<T> extends AbstractJavaType<T> implements MutabilityPlan<T> { | ||
|
||
private final TypeConfiguration typeConfiguration; | ||
|
||
public FormatMapperBasedJavaType( | ||
Type type, | ||
MutabilityPlan<T> mutabilityPlan, | ||
TypeConfiguration typeConfiguration) { | ||
super( type, mutabilityPlan ); | ||
this.typeConfiguration = typeConfiguration; | ||
} | ||
|
||
protected abstract FormatMapper getFormatMapper(TypeConfiguration typeConfiguration); | ||
|
||
@Override | ||
public JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) { | ||
throw new JdbcTypeRecommendationException( | ||
"Could not determine recommended JdbcType for Java type '" + getJavaType().getTypeName() + "'" | ||
); | ||
} | ||
|
||
@Override | ||
public String toString(T value) { | ||
return getFormatMapper( typeConfiguration ).toString( | ||
value, | ||
this, | ||
typeConfiguration.getSessionFactory().getWrapperOptions() | ||
); | ||
} | ||
|
||
@Override | ||
public T fromString(CharSequence string) { | ||
return getFormatMapper( typeConfiguration ).fromString( | ||
string, | ||
this, | ||
typeConfiguration.getSessionFactory().getWrapperOptions() | ||
); | ||
} | ||
|
||
@Override | ||
public <X> X unwrap(T value, Class<X> type, WrapperOptions options) { | ||
if ( type.isAssignableFrom( getJavaTypeClass() ) ) { | ||
//noinspection unchecked | ||
return (X) value; | ||
} | ||
else if ( type == String.class ) { | ||
//noinspection unchecked | ||
return (X) getFormatMapper( typeConfiguration ).toString( value, this, options ); | ||
} | ||
throw new UnsupportedOperationException( | ||
"Unwrap strategy not known for this Java type : " + getJavaType().getTypeName() | ||
); | ||
} | ||
|
||
@Override | ||
public <X> T wrap(X value, WrapperOptions options) { | ||
if ( getJavaTypeClass().isInstance( value ) ) { | ||
//noinspection unchecked | ||
return (T) value; | ||
} | ||
else if ( value instanceof String ) { | ||
return getFormatMapper( typeConfiguration ).fromString( (String) value, this, options ); | ||
} | ||
throw new UnsupportedOperationException( | ||
"Wrap strategy not known for this Java type : " + getJavaType().getTypeName() | ||
); | ||
} | ||
|
||
@Override | ||
public MutabilityPlan<T> getMutabilityPlan() { | ||
final MutabilityPlan<T> mutabilityPlan = super.getMutabilityPlan(); | ||
return mutabilityPlan == null ? this : mutabilityPlan; | ||
} | ||
|
||
@Override | ||
public boolean isMutable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public T deepCopy(T value) { | ||
return fromString( toString( value ) ); | ||
} | ||
|
||
@Override | ||
public Serializable disassemble(T value, SharedSessionContract session) { | ||
return toString( value ); | ||
} | ||
|
||
@Override | ||
public T assemble(Serializable cached, SharedSessionContract session) { | ||
return fromString( (CharSequence) cached ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.