mirror of
https://github.com/corda/corda.git
synced 2025-06-17 14:48:16 +00:00
Add AbstractPartyDescriptor to stop Hibernate warning (#1878)
This commit is contained in:
@ -0,0 +1,68 @@
|
|||||||
|
package net.corda.node.services.persistence
|
||||||
|
|
||||||
|
import net.corda.core.identity.AbstractParty
|
||||||
|
import net.corda.core.identity.CordaX500Name
|
||||||
|
import net.corda.core.node.services.IdentityService
|
||||||
|
import net.corda.core.utilities.loggerFor
|
||||||
|
import org.hibernate.type.descriptor.WrapperOptions
|
||||||
|
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor
|
||||||
|
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan
|
||||||
|
import org.hibernate.type.descriptor.java.MutabilityPlan
|
||||||
|
|
||||||
|
class AbstractPartyDescriptor(identitySvc: () -> IdentityService) : AbstractTypeDescriptor<AbstractParty>(AbstractParty::class.java) {
|
||||||
|
companion object {
|
||||||
|
private val log = loggerFor<AbstractPartyDescriptor>()
|
||||||
|
}
|
||||||
|
|
||||||
|
private val identityService: IdentityService by lazy(identitySvc)
|
||||||
|
|
||||||
|
override fun fromString(dbData: String?): AbstractParty? {
|
||||||
|
return if (dbData != null) {
|
||||||
|
val party = identityService.wellKnownPartyFromX500Name(CordaX500Name.parse(dbData))
|
||||||
|
if (party == null) log.warn("Identity service unable to resolve X500name: $dbData")
|
||||||
|
party
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getMutabilityPlan(): MutabilityPlan<AbstractParty> = ImmutableMutabilityPlan()
|
||||||
|
|
||||||
|
override fun toString(party: AbstractParty?): String? {
|
||||||
|
return if (party != null) {
|
||||||
|
val partyName = party.nameOrNull() ?: identityService.wellKnownPartyFromAnonymous(party)?.name
|
||||||
|
if (partyName == null) log.warn("Identity service unable to resolve AbstractParty: $party")
|
||||||
|
partyName.toString()
|
||||||
|
} else {
|
||||||
|
return null // non resolvable anonymous parties
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun <X : Any> wrap(value: X?, options: WrapperOptions): AbstractParty? {
|
||||||
|
return if (value != null) {
|
||||||
|
if (String::class.java.isInstance(value)) {
|
||||||
|
return fromString(value as String)!!
|
||||||
|
}
|
||||||
|
if (AbstractParty::class.java.isInstance(value)) {
|
||||||
|
return value as AbstractParty
|
||||||
|
}
|
||||||
|
throw unknownWrap(value::class.java)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun <X : Any> unwrap(value: AbstractParty?, type: Class<X>, options: WrapperOptions): X? {
|
||||||
|
return if (value != null) {
|
||||||
|
if (AbstractParty::class.java.isAssignableFrom(type)) {
|
||||||
|
return value as X
|
||||||
|
}
|
||||||
|
if (String::class.java.isAssignableFrom(type)) {
|
||||||
|
return toString(value) as X
|
||||||
|
}
|
||||||
|
throw unknownUnwrap(type)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -18,6 +18,7 @@ import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider
|
|||||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment
|
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment
|
||||||
import org.hibernate.service.UnknownUnwrapTypeException
|
import org.hibernate.service.UnknownUnwrapTypeException
|
||||||
import org.hibernate.type.AbstractSingleColumnStandardBasicType
|
import org.hibernate.type.AbstractSingleColumnStandardBasicType
|
||||||
|
import org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry
|
||||||
import org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor
|
import org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor
|
||||||
import org.hibernate.type.descriptor.sql.BlobTypeDescriptor
|
import org.hibernate.type.descriptor.sql.BlobTypeDescriptor
|
||||||
import java.sql.Connection
|
import java.sql.Connection
|
||||||
@ -35,6 +36,11 @@ class HibernateConfiguration(val schemaService: SchemaService, private val datab
|
|||||||
private val transactionIsolationLevel = parserTransactionIsolationLevel(databaseProperties.getProperty("transactionIsolationLevel") ?: "")
|
private val transactionIsolationLevel = parserTransactionIsolationLevel(databaseProperties.getProperty("transactionIsolationLevel") ?: "")
|
||||||
val sessionFactoryForRegisteredSchemas = schemaService.schemaOptions.keys.let {
|
val sessionFactoryForRegisteredSchemas = schemaService.schemaOptions.keys.let {
|
||||||
logger.info("Init HibernateConfiguration for schemas: $it")
|
logger.info("Init HibernateConfiguration for schemas: $it")
|
||||||
|
// Register the AbstractPartyDescriptor so Hibernate doesn't warn when encountering AbstractParty. Unfortunately
|
||||||
|
// Hibernate warns about not being able to find a descriptor if we don't provide one, but won't use it by default
|
||||||
|
// so we end up providing both descriptor and converter. We should re-examine this in later versions to see if
|
||||||
|
// either Hibernate can be convinced to stop warning, use the descriptor by default, or something else.
|
||||||
|
JavaTypeDescriptorRegistry.INSTANCE.addDescriptor(AbstractPartyDescriptor(createIdentityService))
|
||||||
sessionFactoryForSchemas(it)
|
sessionFactoryForSchemas(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user