我正在尝试将我的自定义 postgres 类型映射到我的休眠实体,而且我正在将 Quarkus 与 kotlin 和 panache 存储库一起使用。
-> 我的 postgres 设置...
CREATE TYPE money_and_currency AS (amount integer, currency varchar(3));
CREATE TABLE example_table (
"id" uuid,
"amount" money_and_currency,
);
-> 我的 Hibernate 实体设置...
@Entity
@Table(name = "example_table")
class ScheduledTransactionEntity : PanacheEntityBase {
@Id
@GeneratedValue
@Column(columnDefinition = "uuid", updatable = false)
@Column(nullable = false)
@Embedded
var amount: MoneyAndCurrency? = null
}
@Embeddable
data class MoneyAndCurrency(val amount: Int, val currency: String)
当我尝试将新数据插入数据库时出现此错误:
org.hibernate.exception.SQLGrammarException:无法执行语句[错误:关系“scheduled_transactions”的列“currency”不存在
我还尝试向@Column注释添加columnDefinition =“money_and_currency”,但没有任何改变......
我只需要知道如何使用 hibernate 正确映射复合自定义类型。
实体是正确的,但它缺少 @Struct(name = "money_and_currency") 注释来告诉 hibernate 数据库中声明的类型是什么,hibernate 应该映射到@Entity
@Table(name = "example_table")
class ScheduledTransactionEntity : PanacheEntityBase {
@Id
@GeneratedValue
@Column(columnDefinition = "uuid", updatable = false)
@Column(nullable = false)
@Embedded
var amount: MoneyAndCurrency? = null
}
@Embeddable
@Struct(name = "money_and_currency")
data class MoneyAndCurrency(val amount: Int, val currency: String)