-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement EnhancedUserType in a new DescriptorImmutableType in Hibern…
…ate 6 modules #633
- Loading branch information
1 parent
4887e9c
commit 5fe3d3b
Showing
59 changed files
with
840 additions
and
116 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...rnate-60/src/main/java/io/hypersistence/utils/hibernate/type/DescriptorImmutableType.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,73 @@ | ||
package io.hypersistence.utils.hibernate.type; | ||
|
||
import io.hypersistence.utils.hibernate.type.util.Configuration; | ||
import org.hibernate.HibernateException; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.query.BindableType; | ||
import org.hibernate.query.sqm.SqmExpressible; | ||
import org.hibernate.type.descriptor.java.JavaType; | ||
import org.hibernate.type.descriptor.jdbc.JdbcType; | ||
import org.hibernate.usertype.UserType; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* Very convenient base class for implementing immutable object types using Hibernate {@link UserType} using the {@link JdbcType} and {@link JavaType} descriptors. | ||
* | ||
* @author Vlad Mihalcea | ||
*/ | ||
public abstract class DescriptorImmutableType<T, JDBC extends JdbcType, JAVA extends JavaType<T>> extends ImmutableType<T> implements BindableType<T>, SqmExpressible<T> { | ||
|
||
private final JDBC jdbcTypeDescriptor; | ||
private final JAVA javaTypeDescriptor; | ||
|
||
public DescriptorImmutableType(Class<T> clazz, JDBC jdbcTypeDescriptor, JAVA javaTypeDescriptor) { | ||
super(clazz); | ||
this.jdbcTypeDescriptor = jdbcTypeDescriptor; | ||
this.javaTypeDescriptor = javaTypeDescriptor; | ||
} | ||
|
||
public DescriptorImmutableType(Class<T> clazz, JDBC jdbcTypeDescriptor, JAVA javaTypeDescriptor, Configuration configuration) { | ||
super(clazz, configuration); | ||
this.jdbcTypeDescriptor = jdbcTypeDescriptor; | ||
this.javaTypeDescriptor = javaTypeDescriptor; | ||
} | ||
|
||
@Override | ||
public T nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws | ||
SQLException { | ||
return jdbcTypeDescriptor.getExtractor(javaTypeDescriptor).extract(rs, position, session); | ||
} | ||
|
||
@Override | ||
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { | ||
jdbcTypeDescriptor.getBinder(javaTypeDescriptor).bind(st, (T) value, index, session); | ||
} | ||
|
||
@Override | ||
protected T get(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { | ||
return nullSafeGet(rs, position, session, owner); | ||
} | ||
|
||
@Override | ||
protected void set(PreparedStatement st, T value, int index, SharedSessionContractImplementor session) throws SQLException { | ||
nullSafeSet(st, value, index, session); | ||
} | ||
|
||
@Override | ||
public int getSqlType() { | ||
return jdbcTypeDescriptor.getJdbcTypeCode(); | ||
} | ||
|
||
@Override | ||
public Class<T> getBindableJavaType() { | ||
return returnedClass(); | ||
} | ||
|
||
@Override | ||
public JavaType<T> getExpressibleJavaType() { | ||
return javaTypeDescriptor; | ||
} | ||
} |
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
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
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
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.