I found a few solutions using java, but none of them seems to work after I convert the code to Kotlin. By the error code, I know that it is complaining about the fields with @Type(type = "string-array") at the Entity.
But is it possible to solve it using Kotlin?
How I can solve this error No Dialect mapping for JDBC type: 2003?
Solution that I tried: https://vladmihalcea.com/hibernate-no-dialect-mapping-for-jdbc-type/
Repository
interface SchemaRepository : CrudRepository<SchemaEntity, Long> {
fun save(schema: SchemaEntity): SchemaEntity
}
Entity
@Entity
@Table(name = "schemas")
@TypeDefs(
TypeDef(name = "jsonb", typeClass = JsonBinaryType::class),
TypeDef(name = "string-array", typeClass = StringArrayType::class)
)
class SchemaEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@Type(type = "string")
@Column(name = "name", nullable = false, unique = true)
val name: String,
@Type(type = "jsonb")
@Column(name = "schema", nullable = false)
val schema: Schema,
@Type(type = "jsonb")
@Column(name = "output_schema")
val outputSchema: Schema?,
@Type(type = "string-array")
@Column(name = "tags")
val tags: Array<String>?,
@Type(type = "string")
@Column(name = "owner")
val owner: String?,
@Type(type = "timestamp")
@Column(name = "created_at")
val createdAt: Timestamp? = Timestamp(System.currentTimeMillis()),
@Type(type = "timestamp")
@Column(name = "updated_at")
val updatedAt: Timestamp? = Timestamp(System.currentTimeMillis()),
)
Custom dialect
Located at test folder.
package com.dummy.repository.dialect
import com.vladmihalcea.hibernate.type.array.StringArrayType
import com.vladmihalcea.hibernate.type.json.JsonNodeBinaryType
import org.hibernate.dialect.PostgreSQL10Dialect
import java.sql.Types
class PostgreSQL10JsonDialect : PostgreSQL10Dialect() {
init {
this.registerHibernateType(Types.OTHER, JsonNodeBinaryType::class.java.name)
this.registerHibernateType(Types.ARRAY, StringArrayType::class.java.name)
}
}
test/resources/application.properties
spring.datasource.url = jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.initialize=true
spring.jpa.properties.hibernate.dialect=com.dummy.dataportalapi.repository.dialect.PostgreSQL10JsonDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
Example of failing test
import com.dummy.dto.Schema
import com.dummy.model.SchemaEntity
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.data.repository.findByIdOrNull
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.transaction.annotation.Transactional
import java.sql.Timestamp
@ExtendWith(SpringExtension::class)
@DataJpaTest
@Transactional
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class SchemaRepositoryTest {
@Autowired
private val repository: SchemaRepository? = null
val first = createDummySchemaEntity("First schema")
val second = createDummySchemaEntity("Second schema")
val third = createDummySchemaEntity("Third schema")
@Nested
inner class `save schema` {
@Test
fun `with success`() {
val saved = repository?.save(first)
val id = saved?.id ?: throw Exception("Not found ID on test data")
val obtained = repository?.findByIdOrNull(id)
validateEntityContent(first, obtained)
}
}}
Obtained error
Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
.
.
.
Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: 2003