簡體   English   中英

在 Hibernate 中的復合 @JoinColumns 上使用 AttribueConverter

[英]Using AttribueConverter on a composite @JoinColumns in Hibernate

Hibernate(帶有 Spring Boot JPA)在目標實體使用復合連接時無法插入,其中其中一列是使用轉換器的自定義類型。

簡化的實體定義:

@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
)

轉換器 class 使用簡單的字符串操作, TableId class 實現了Serializable 從 Spring Boot 應用程序的上下文中保存元素時,出現以下異常:

... 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 ...

我驗證了將TableId列更改為簡單字符串可以解決問題。 我還應該申請其他任何東西以使其與此連接查詢一起使用嗎?

在這種情況下,id 或 id 的一部分上的轉換器只是偶然工作。 有一個未解決的問題: https://hibernate.atlassian.net/browse/HHH-8820

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM