Home > Mobile >  Using AttribueConverter on a composite @JoinColumns in Hibernate
Using AttribueConverter on a composite @JoinColumns in Hibernate

Time:10-01

Hibernate (with Spring Boot JPA) fails to insert when the target entity is using a composite join where one of the columns is a custom type that uses a converter.

A simplified entities definition:

@Entity
data class MyConfiguration(
    @Id
    val configurationId: Long,
    val enabled: Boolean,
    @OneToMany(
        cascade = [CascadeType.ALL],
        fetch = FetchType.EAGER,
        orphanRemoval = true,
        mappedBy = "id.configurationId"
    )
    val tables: Set<SourceTable>
)

@Embeddable
data class SourceTableId(
    val configurationId: Long,
    @Convert(converter = TableIdConverter::class)
    val tableId: TableId
) : Serializable

@Entity
data class SourceTable(
    @EmbeddedId
    val id: SourceTableId,
    @OneToMany(
        cascade = [CascadeType.ALL],
        fetch = FetchType.EAGER,
        orphanRemoval = true
    )
    @JoinColumns(
        JoinColumn(name = "configurationId", referencedColumnName = "configurationId", updatable = false),
        JoinColumn(name = "tableId", referencedColumnName = "tableId", updatable = false)
    )
    val groupingFields: Set<TableGroupField> = emptySet()
)

@Embeddable
data class TableGroupFieldId(
    val configurationId: Long,
    @Convert(converter = TableIdConverter::class)
    val tableId: TableId,
    val fieldName: String
) : Serializable

@Entity
data class TableGroupField(
    @EmbeddedId
    val id: TableGroupFieldId
)

The converter class is using simple string operations, and the TableId class implements Serializable. When saving an element from the context of a Spring Boot application, I'm getting the following exception:

... Omitted for readability ...
Caused by: javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.internal.ExceptionConverterImpl.convertCommitException(ExceptionConverterImpl.java:81) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:104) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:562) ~[spring-orm-5.3.8.jar:5.3.8]
    ... 33 common frames omitted
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.Comparator.compare(Object, Object)" because the return value of "org.hibernate.type.descriptor.java.JavaTypeDescriptor.getComparator()" is null
    at org.hibernate.type.AbstractStandardBasicType.compare(AbstractStandardBasicType.java:211) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.type.ComponentType.compare(ComponentType.java:217) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
... Omitted for readability ...

I verified that changing the TableId column to a simple string resolves the issue. Is there anything else I should apply to make it work with this join query?

CodePudding user response:

Converters on the id or parts of the id only work by accident in this case. There is an open issue for this: https://hibernate.atlassian.net/browse/HHH-8820

  • Related