Created
April 11, 2023 16:39
-
-
Save john-evangelist/ad73a27d4cedbc339b01e7ae7054ba39 to your computer and use it in GitHub Desktop.
Custom Type to allow Oracle Char to be used on JPA without the need to trim
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
import com.zaxxer.hikari.pool.HikariProxyPreparedStatement | |
import oracle.jdbc.OraclePreparedStatement | |
import org.hibernate.engine.spi.SharedSessionContractImplementor | |
import org.hibernate.usertype.UserType | |
import java.io.Serializable | |
import java.sql.PreparedStatement | |
import java.sql.ResultSet | |
import java.sql.Types | |
class OracleCharType : UserType<String> { | |
override fun equals(x: String?, y: String?): Boolean { | |
return x == y; | |
} | |
override fun hashCode(x: String?): Int { | |
return x.hashCode() | |
} | |
override fun getSqlType(): Int { | |
return Types.CHAR | |
} | |
override fun returnedClass(): Class<String> { | |
return String::class.java | |
} | |
override fun nullSafeGet(rs: ResultSet?, position: Int, session: SharedSessionContractImplementor?, owner: Any?): String? { | |
return rs?.getString(position)?.trim() | |
} | |
override fun isMutable(): Boolean { | |
return false | |
} | |
override fun assemble(cached: Serializable?, owner: Any?): String? { | |
return cached as String? | |
} | |
override fun replace(detached: String?, managed: String?, owner: Any?): String? { | |
return detached | |
} | |
override fun disassemble(value: String?): Serializable { | |
return value as Serializable | |
} | |
override fun deepCopy(value: String?): String? { | |
return value | |
} | |
override fun nullSafeSet(st: PreparedStatement?, value: String?, index: Int, session: SharedSessionContractImplementor?) { | |
val dbPoolSt = st as HikariProxyPreparedStatement | |
val ost = dbPoolSt.unwrap(OraclePreparedStatement::class.java) | |
ost.setFixedCHAR(index, value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment