Skip to content

Instantly share code, notes, and snippets.

@Ultranium
Created August 25, 2022 15:39
Show Gist options
  • Save Ultranium/2caaa4899ad117647f59abf690389dcd to your computer and use it in GitHub Desktop.
Save Ultranium/2caaa4899ad117647f59abf690389dcd to your computer and use it in GitHub Desktop.
UserType implementation for the Vert.x JsonObject class for use with Hibernate Reactive
import io.vertx.core.json.JsonObject
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
import java.util.*
/**
* A UserType implementation for the Vert.x JsonObject class for use with Hibernate Reactive and Reactive PostgreSQL client.
* Tested on Quarkus 2.11.3.
*
* Based on https://github.com/hibernate/hibernate-reactive/blob/1aa452c050a989e72837b3f375e4e8a2673977ff/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/Json.java
*/
class JsonBinaryType : UserType {
private val sqlTypes = intArrayOf(Types.OTHER)
override fun equals(x: Any?, y: Any?): Boolean = Objects.equals(x, y)
override fun hashCode(x: Any?): Int = Objects.hashCode(x)
override fun sqlTypes(): IntArray = sqlTypes
override fun returnedClass(): Class<*> = JsonObject::class.java
override fun nullSafeGet(
rs: ResultSet,
names: Array<out String>,
session: SharedSessionContractImplementor,
owner: Any
): Any? = rs.getObject(names[0]) as JsonObject?
override fun nullSafeSet(
st: PreparedStatement,
value: Any?,
index: Int,
session: SharedSessionContractImplementor
) {
if (value != null)
st.setObject(index, value)
else
st.setNull(index, Types.OTHER)
}
override fun deepCopy(value: Any?): Any? = (value as JsonObject?)?.copy()
override fun isMutable(): Boolean = true
override fun disassemble(value: Any?): Serializable? = (value as JsonObject?)?.encode()
override fun assemble(cached: Serializable?, owner: Any): Any? {
return if (cached != null)
JsonObject(cached as String)
else
null
}
override fun replace(original: Any?, target: Any?, owner: Any?): Any? = (original as JsonObject?)?.copy()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment